I'm posting this for reference / advice if anyone can tell me what I did or didn't do wrong. (it works though)
include \masm32\include\masm32rt.inc
.data
numary sdword 2,5,7,8,9,3,6,1
zeroary sdword 3 dup(0)
.data?
empary sdword ?,?,?
.const
.code
start:
main proc
mov ebx, 0
mov ecx, lengthof numary ; get amount of elements in array
.while ecx > 0 ; while ecx is more than 0 continue loop
push ecx ; save ecx for later since the following macro might destroy (ecx)
print str$([numary+ebx]), 0Ah
add ebx, 4 ; add 4 bytes to access each element of array
.if ecx<=0 ; if ecx is less than or equal to zero then jump to finish
jmp @fin
.endif
pop ecx ; pop saved value back into ecx
dec ecx ; decrement ecx (amount of elements in array) loop until 0
.endw
@fin:
inkey "Press any key to exit"
ret
main endp
end start
variation 1
include \masm32\include\masm32rt.inc
.data
numary sdword 2,5,7,8,9,3,6,1
zeroary sdword 3 dup(0)
.data?
empary sdword ?,?,?
.const
.code
start:
main proc
mov ecx, lengthof numary
lea esi, numary+0
.repeat
push ecx
print str$([esi]), 0Ah
add esi, 4
.if ecx<=0
jmp @fin
.endif
pop ecx
.untilcxz
@fin:
inkey "Press any key to exit"
ret
main endp
end start
variation 2
I'm pretty sure my c++ comparison is right.. if not let me know and i'll change it to save confusion.
include \masm32\include\masm32rt.inc
.data
numary sdword 2,5,7,8,9,3,6,1
zeroary sdword 3 dup(0)
.data?
empary sdword ?,?,?
.const
.code
start:
main proc
;offset = actual address
;addr = value of addr
;&esi (c++) - esi (no brackets) - address
;*esi (c++) - [esi] - value of address esi
mov ecx, lengthof numary
mov esi, offset numary+0
.repeat
push ecx
print str$([esi]), 0Ah ; print value of esi
add esi, 4 ; each element
.if ecx<=0
jmp @fin ; jump if done
.endif
pop ecx
.untilcxz ; auto decrement ecx until zero
@fin:
inkey "Press any key to exit"
ret
main endp
end start
Quote
push ecx
print str$([esi]), 0Ah ; print value of esi
add esi, 4 ; each element
.if ecx<=0
pop ecx ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<< needed
jmp @fin ; jump if done
.endif
pop ecx
@fin
You have forgotten the pop ecx.
To avoid this sort of mistake,you can use local variable and decrease it.
push ecx
print str$([esi]), 0Ah ; print value of esi
add esi, 4 ; each element
.if ecx<=0
jmp @fin ; jump if done
.endif
@fin
pop ecx ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<< or just move this one to AFTER @fin
personally, i would write the loop without all the indented code and if/else/endif :P
TopOfLoop:
push ecx
print str$([esi]),0Dh,0Ah ; print value of [esi]
pop ecx
add esi,4
dec ecx
jnz TopOfLoop
there, now isn't that easier to understand ?