Script started on Thu 29 Feb 2024 10:55:35 AM EST [mweeks@gsuad.gsu.edu@snowball ~]$ echo This is like array_sum.asm, but uses This is like array_sum.asm, but uses [mweeks@gsuad.gsu.edu@snowball ~]$ echo a duplicate pointer to arrayW. a duplicate pointer to arrayW. [mweeks@gsuad.gsu.edu@snowball ~]$ echo Look at how "ptrW" is defined in the next examples. Look at how ptrW is defined in the next examples. [mweeks@gsuad.gsu.edu@snowball ~]$ cat array_sum4a.asm ; Assemble: nasm -f elf64 array_sum4a.asm ; Link: gcc array_sum4a.o -o array_sum4a ; Based on code from Irvine's chapter 4. ; See chapter 4 slide 62. ; This is adapted for NASM. -MCW extern printf ; We will use this external function section .data ; Data section, initialized variables mystr: db "ptrW is %x", 10, 0 ; format to use arrayW: dw 1000h, 2000h, 3000h ; The next line does not work. Assembler expects more. ptrW: arrayW mystr1: db "The array sum is %x (hex)", 10, 0 mystr2a: db "Value 0 is %x", 10, 0 mystr2b: db "Value 1 is %x", 10, 0 mystr3: db "arrayW has address %x", 10, 0 mystr4: db "ptrW has address %x", 10, 0 sum: dw 0 temp_ptr: dq 0 section .text global main main: xor rax, rax ; A = 0 ; Notice how we use a "pointer" to arrayW mov rsi, ptrW mov ax, word [rsi] ; get the first word (different way to access) ;mov ax, [ptrW] ; get the first word (different way to access) mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2a ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 2nd word mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2b ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 3rd word mov [sum], ax ; store the sum so far mov rsi, rax ; print final sum mov rax, 0 mov rdi, mystr1 ; call printf ; print the arrayW address mov rsi, arrayW mov rax, 0 mov rdi, mystr3 ; call printf ; print the ptrW value mov rsi, ptrW mov rax, 0 mov rdi, mystr4 ; call printf mov rax, 0 ret [mweeks@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 array_sum4a.asm array_sum4a.asm:16: error: parser: instruction expected [mweeks@gsuad.gsu.edu@snowball ~]$ echo This is supposed to be a NASM version of the example on p62 of ch 4 slides, but it does not work. This is supposed to be a NASM version of the example on p62 of ch 4 slides, but it does not work. [mweeks@gsuad.gsu.edu@snowball ~]$ echo As you can see, the assembler has a problem with the "ptrW: arrayW" line. As you can see, the assembler has a problem with the ptrW: arrayW line. [mweeks@gsuad.gsu.edu@snowball ~]$ echo In the slide, it uses DWORD on that line. We can try DWORD here, but it does not work. In the slide, it uses DWORD on that line. We can try DWORD here, but it does not work. [mweeks@gsuad.gsu.edu@snowball ~]$ echo We can try dq instead, which will assemble. We can try dq instead, which will assemble. [mweeks@gsuad.gsu.edu@snowball ~]$ cat array_sum4b.asm ; Assemble: nasm -f elf64 array_sum4b.asm ; Link: gcc array_sum4b.o -o array_sum4b ; Based on code from Irvine's chapter 4. ; See chapter 4 slide 62. ; This is adapted for NASM. -MCW extern printf ; We will use this external function section .data ; Data section, initialized variables mystr: db "ptrW is %x", 10, 0 ; format to use arrayW: dw 1000h, 2000h, 3000h ; The next line does not work. Assembler expects more. ;ptrW: arrayW ptrW: dq arrayW ; However, the "dq arrayW" does not quite work either. ; The dq may say "ptrW starts here and has 64 bits", ; i.e. not create a duplicate pointer to arrayW. ; What it does is put the address of arrayW in memory here. ; In other words, "ptrW" is not equivalent to "arrayW", ; but "[ptrW]" is equivalent to "arrayW". mystr1: db "The array sum is %x (hex)", 10, 0 mystr2a: db "Value 0 is %x", 10, 0 mystr2b: db "Value 1 is %x", 10, 0 mystr3: db "arrayW has address %x", 10, 0 mystr4: db "ptrW has address %x", 10, 0 sum: dw 0 temp_ptr: dq 0 section .text global main main: xor rax, rax ; A = 0 ; Notice how we use a "pointer" to arrayW mov rsi, ptrW mov ax, word [rsi] ; get the first word (different way to access) ; mov ax, [ptrW] ; get the first word (different way to access) mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2a ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 2nd word mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2b ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 3rd word mov [sum], ax ; store the sum so far mov rsi, rax ; print final sum mov rax, 0 mov rdi, mystr1 ; call printf ; print the arrayW address mov rsi, arrayW mov rax, 0 mov rdi, mystr3 ; call printf ; print the ptrW value mov rsi, ptrW mov rax, 0 mov rdi, mystr4 ; call printf mov rax, 0 ret [mweeks@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 array_sum4b.asm [mweeks@gsuad.gsu.edu@snowball ~]$ gcc array_sum4b.o -o array_sum4b [mweeks@gsuad.gsu.edu@snowball ~]$ ./array_sum4b Value 0 is 1040 Value 1 is 10a0 The array sum is 10a0 (hex) arrayW has address 601040 ptrW has address 601046 [mweeks@gsuad.gsu.edu@snowball ~]$ echo This looks like it works, but the values and sum do not match what they should be. This looks like it works, but the values and sum do not match what they should be. [mweeks@gsuad.gsu.edu@snowball ~]$ echo Notice also how ptrW has a different address from arrayW. Notice also how ptrW has a different address from arrayW. [mweeks@gsuad.gsu.edu@snowball ~]$ echo The whole point about the example was to show how they would point to the same memory. The whole point about the example was to show how they would point to the same memory. [mweeks@gsuad.gsu.edu@snowball ~]$ cat array_sum4c.asm ; Assemble: nasm -f elf64 array_sum4c.asm ; Link: gcc array_sum4c.o -o array_sum4c ; Based on code from Irvine's chapter 4. ; See chapter 4 slide 62. ; This is adapted for NASM. -MCW extern printf ; We will use this external function section .data ; Data section, initialized variables mystr: db "ptrW is %x", 10, 0 ; format to use ptrW: ; This works. arrayW: dw 1000h, 2000h, 3000h ; The next line does not work. Assembler expects more. ;ptrW: arrayW ;ptrW: dq arrayW ; However, the "dq arrayW" does not quite work either. ; The dq may say "ptrW starts here and has 64 bits", ; i.e. not create a duplicate pointer to arrayW. ; What it does is put the address of arrayW in memory here. ; In other words, "ptrW" is not equivalent to "arrayW", ; but "[ptrW]" is equivalent to "arrayW". mystr1: db "The array sum is %x (hex)", 10, 0 mystr2a: db "Value 0 is %x", 10, 0 mystr2b: db "Value 1 is %x", 10, 0 mystr3: db "arrayW has address %x", 10, 0 mystr4: db "ptrW has address %x", 10, 0 sum: dw 0 temp_ptr: dq 0 section .text global main main: xor rax, rax ; A = 0 ; Notice how we use a "pointer" to arrayW mov rsi, ptrW mov ax, word [rsi] ; get the first word (different way to access) ; mov ax, [ptrW] ; get the first word (different way to access) mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2a ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 2nd word mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2b ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 3rd word mov [sum], ax ; store the sum so far mov rsi, rax ; print final sum mov rax, 0 mov rdi, mystr1 ; call printf ; print the arrayW address mov rsi, arrayW mov rax, 0 mov rdi, mystr3 ; call printf ; print the ptrW value mov rsi, ptrW ;mov rsi, [ptrW] mov rax, 0 mov rdi, mystr4 ; call printf mov rax, 0 ret [mweeks@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 array_sum4c.asm [mweeks@gsuad.gsu.edu@snowball ~]$ gcc array_sum4c.o -o array_sum4c [mweeks@gsuad.gsu.edu@snowball ~]$ ./array_sum4c Value 0 is 1000 Value 1 is 3000 The array sum is 6000 (hex) arrayW has address 601040 ptrW has address 601040 [mweeks@gsuad.gsu.edu@snowball ~]$ echo The values are correct, the sum is what we expect, and we see that the two labels do point to the same memory. The values are correct, the sum is what we expect, and we see that the two labels do point to the same memory. [mweeks@gsuad.gsu.edu@snowball ~]$ cp array_sum4b.asm array_sum4d.asm [mweeks@gsuad.gsu.edu@snowball ~]$ vi array_sum4d.asm [mweeks@gsuad.gsu.edu@snowball ~]$ cat array_sum4d.asm ; Assemble: nasm -f elf64 array_sum4d.asm ; Link: gcc array_sum4d.o -o array_sum4d ; Based on code from Irvine's chapter 4. ; See chapter 4 slide 62. ; This is adapted for NASM. -MCW extern printf ; We will use this external function section .data ; Data section, initialized variables mystr: db "ptrW is %x", 10, 0 ; format to use arrayW: dw 1000h, 2000h, 3000h ; The next line does not work. Assembler expects more. ;ptrW: arrayW ptrW: dq arrayW ; However, the "dq arrayW" does not quite work either. ; The dq may say "ptrW starts here and has 64 bits", ; i.e. not create a duplicate pointer to arrayW. ; What it does is put the address of arrayW in memory here. ; In other words, "ptrW" is not equivalent to "arrayW", ; but "[ptrW]" is equivalent to "arrayW". mystr1: db "The array sum is %x (hex)", 10, 0 mystr2a: db "Value 0 is %x", 10, 0 mystr2b: db "Value 1 is %x", 10, 0 mystr3: db "arrayW has address %x", 10, 0 mystr4: db "ptrW has address %x", 10, 0 sum: dw 0 temp_ptr: dq 0 section .text global main main: xor rax, rax ; A = 0 ; Notice how we use a "pointer" to arrayW mov rsi, ptrW mov ax, word [rsi] ; get the first word (different way to access) ; mov ax, [ptrW] ; get the first word (different way to access) mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2a ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 2nd word mov [sum], ax ; store the sum so far mov [temp_ptr], rsi ; store the pointer for later mov rsi, rax ; print mov rax, 0 mov rdi, mystr2b ; call printf mov rsi, [temp_ptr] ; get the previous rsi value mov ax, [sum] ; get the previous ax value add rsi, 2 ; increment the "pointer" add ax, [rsi] ; add the 3rd word mov [sum], ax ; store the sum so far mov rsi, rax ; print final sum mov rax, 0 mov rdi, mystr1 ; call printf ; print the arrayW address mov rsi, arrayW mov rax, 0 mov rdi, mystr3 ; call printf ; print the ptrW value mov rsi, [ptrW] mov rax, 0 mov rdi, mystr4 ; call printf mov rax, 0 ret [mweeks@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 array_sum4d.asm [mweeks@gsuad.gsu.edu@snowball ~]$ gcc array_sum4d.o -o array_sum4d [mweeks@gsuad.gsu.edu@snowball ~]$ ./array_sum4d Value 0 is 1040 Value 1 is 10a0 The array sum is 10a0 (hex) arrayW has address 601040 ptrW has address 601040 [mweeks@gsuad.gsu.edu@snowball ~]$ echo This is a revised version of array_sum4b.asm This is a revised version of array_sum4b.asm [mweeks@gsuad.gsu.edu@snowball ~]$ echo Notice how the "mov rsi, [ptrW]" is different from before. Notice how the mov rsi, [ptrW] is different from before. [mweeks@gsuad.gsu.edu@snowball ~]$ echo Also, you should see how the arrayW and ptrW have the same value. Also, you should see how the arrayW and ptrW have the same value. [mweeks@gsuad.gsu.edu@snowball ~]$ echo In other words, ptrW is not a duplicate label, In other words, ptrW is not a duplicate label, [mweeks@gsuad.gsu.edu@snowball ~]$ echo but a second pointer to arrayW. but a second pointer to arrayW. [mweeks@gsuad.gsu.edu@snowball ~]$ echo ptrW is not interchangeable with arrayW. It has its own memory and starts ptrW is not interchangeable with arrayW. It has its own memory and starts [mweeks@gsuad.gsu.edu@snowball ~]$ echo holding a value that is equal to the address of arrayW. holding a value that is equal to the address of arrayW. [mweeks@gsuad.gsu.edu@snowball ~]$ echo We could change ptrW without affecting arrayW. We could change ptrW without affecting arrayW. [mweeks@gsuad.gsu.edu@snowball ~]$ echo By the way, the output text "ptrW has address 601040" is not correct. By the way, the output text ptrW has address 601040 is not correct. [mweeks@gsuad.gsu.edu@snowball ~]$ echo It should be something like "ptrW has value 601040". That value is the same as the address of arrayW. It should be something like ptrW has value 601040. That value is the same as the address of arrayW. [mweeks@gsuad.gsu.edu@snowball ~]$ exit exit Script done on Thu 29 Feb 2024 11:12:36 AM EST