Search code examples
arraysloopsassemblymasm32

assembly - accessing array elements


I have an array that I am attempting to print. I would like to print it out so I can see if it is correct. It is currently printing the number 1 and stopping. Or, if I mess with the ECX differently it prints out a bunch of zeros and crashes.

Here is my program.

.data

array DWORD 10 DUP(5, 7, 6, 1, 4, 3, 9, 2, 10, 8)
my_size dd 10

sorted DWORD 0
first DWORD 0
second DWORD 0

.code

start:
main proc
cls

 mov EBX, offset[array]
 mov ECX, [my_size]
 dec ECX
 sub ESI, ESI
 sub EDI, EDI

; print
mov EBX, offset aa
sub ECX, ECX
;mov ECX, my_size
mov ECX, 10

my_loop:
mov EAX, [EBX]
inc EBX
dec ECX

cmp ECX, 0
jle exit_loop

mov first, EAX
print chr$("printing array elements: ")
print str$(first)

loop  my_loop

exit_loop:
ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

Solution

  • I see you've simplified the code. Good idea! I'm still not familiar with the macro you're using, "print_str$". That doesn't "look" to me like it prints a number. Have you got a "print_int$" or similar? If you can get it to print just the first number "5", that would be a good start.

    Now... working through your loop, you just "inc ebx". That won't get you the next dword, it'll get you bytes 2, 3, and 4 from the first dword and the first byte from the second dword. Since you used "* 4" in the (removed) sort code, you probably want "[ebx * 4]" here. Either that, or add 4 to ebx each time through the loop. One or the other (but not both) should step through an array of dwords.

    I suspect that the first step would be to select the "right" macro to print a number. It'll probably get easier from there(?). Courage! :)

    Best, Frank