Script started on Wed 28 Feb 2024 10:04:18 PM EST [mweeks@gsuad.gsu.edu@snowball ~]$ cat array_sum.asm ; Assemble: nasm -f elf64 array_sum.asm ; Link: gcc array_sum.o -o array_sum ; Based on code from Irvine's chapter 4. ; This is adapted for NASM. -MCW extern printf ; We will use this external function extern puts ; We will use this external function section .data ; Data section, initialized variables mystr: db "%x", 10, 0 ; String format to use (decimal), followed by NL arrayW: dw 1000h, 2000h, 3000h mystr1: db "The array sum is %d", 10, 0 mystr2: db "Is this the value that you expected?", 10, 0 sum: dw 0 section .text global main main: xor rax, rax ; A = 0 ; Put the address of array into RSI mov rsi, arrayW mov ax, [rsi] ; get the first word add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 2nd word add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 3rd word mov [sum], ax ; store the sum mov rsi, rax mov rax, 0 mov rdi, mystr1 ; call printf mov rdi, mystr2 call puts ; puts adds a newline. So this will print 2 of them. mov rax, 0 ret [mweeks@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 array_sum.asm [mweeks@gsuad.gsu.edu@snowball ~]$ gcc array_sum.o -o array_sum [mweeks@gsuad.gsu.edu@snowball ~]$ ./array_sum The array sum is 24576 Is this the value that you expected? [mweeks@gsuad.gsu.edu@snowball ~]$ echo value is not right... value is not right... [mweeks@gsuad.gsu.edu@snowball ~]$ cat array_sum2.asm ; Assemble: nasm -f elf64 array_sum2.asm ; Link: gcc array_sum2.o -o array_sum2 ; Based on code from Irvine's chapter 4. ; This is adapted for NASM. -MCW extern printf ; We will use this external function section .data ; Data section, initialized variables mystr: db "%x", 10, 0 ; String format to use (decimal), followed by NL arrayW: dw 1000h, 2000h, 3000h mystr1: db "The array sum is %x (hex)", 10, 0 sum: dw 0 section .text global main main: xor rax, rax ; A = 0 ; Put the address of array into RSI mov rsi, arrayW mov ax, [rsi] ; get the first word add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 2nd word add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 3rd word mov [sum], ax ; store the sum mov rsi, rax mov rax, 0 mov rdi, mystr1 ; call printf mov rax, 0 ret [mweeks@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 array_sum2.asm [mweeks@gsuad.gsu.edu@snowball ~]$ gcc array_sum2.o -o array_sum2 [mweeks@gsuad.gsu.edu@snowball ~]$ ./array_sum2 The array sum is 6000 (hex) [mweeks@gsuad.gsu.edu@snowball ~]$ echo this works this works [mweeks@gsuad.gsu.edu@snowball ~]$ cat array_sum3.asm ; Assemble: nasm -f elf64 array_sum3.asm ; Link: gcc array_sum3.o -o array_sum3 ; Based on code from Irvine's chapter 4. ; This is adapted for NASM. -MCW extern printf ; We will use this external function section .data ; Data section, initialized variables mystr: db "%x", 10, 0 ; String format to use (decimal), followed by NL arrayW: dw 1000h, 2000h, 3000h mystr1: db "The array sum is %x (hex)", 10, 0 sum: dw 0 section .text global main main: xor rax, rax ; A = 0 mov rsi, 0 mov ax, [arrayW + rsi] ; get the first word (different way to access) add rsi, 2 ; increment the "pointer" add ax, [arrayW + rsi] ; add the 2nd word add rsi, 2 ; increment the "pointer" add ax, [arrayW + rsi] ; add the 3rd word mov [sum], ax ; store the sum mov rsi, rax mov rax, 0 mov rdi, mystr1 ; call printf mov rax, 0 ret [mweeks@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 array_sum3.asm [mweeks@gsuad.gsu.edu@snowball ~]$ gcc array_sum3.o -o array_sum3 [mweeks@gsuad.gsu.edu@snowball ~]$ ./array_sum3 The array sum is 6000 (hex) [mweeks@gsuad.gsu.edu@snowball ~]$ echo array_sum3 shows another way to access the data array_sum3 shows another way to access the data [mweeks@gsuad.gsu.edu@snowball ~]$ exit exit Script done on Wed 28 Feb 2024 10:06:17 PM EST