Hi, I just started using MASM and I have a couple quick questions.
First off here is my Code:
.686P
.model flat,stdcall
option casemap:none
include \masm32\include\msvcrt.inc
includelib \masm32\lib\msvcrt.lib
.data
MyArray dword 10,28,39,46,70,93,94,888,1000,2000
Count dword 0
szFmt db 'Count=%d Element=%d' ,0
.code
start:
begin:
mov EAX, Count
displayloop:
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
inc EAX
inc Count
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
invoke crt__getch ; getch()
; add EAX, 1
; cmp EAX, 10
; jb displayloop
; invoke crt__getch ; getch()
exitloop: ; end of program
ret
end start
I'm just trying to make an array and then display each element to the screen. I was trying to use a loop with jb but my values for EAX are crazy so the loop doesn't work. So I just tried this but I'm still getting random garbage numbers for the array elements. I think I'm missing something really obvious.
Also, what's the easiest way to add a newline after each printf. I tried \n, \r and their combined variations and nothing worked.
For reference here is the output I am getting :
Count=0 Element=10Count=1 Element=93 Count=2 Element=93 Count=3 Element=93 Count=4 Element=93 Count=5 Element=93 Count=6 Element=93 Count=7 Element=93 Count=8 Element=93 Count=9 Element=93
each time you invoke crt_printf, the contents of the EAX register are destroyed
you can push/pop on each invoke...
inc EAX
inc Count
push eax
invoke crt_printf,addr szFmt, Count, MyArray[EAX] ; printf()
pop eax
in this case, Count and EAX are the same - no need to keep the value in both places
Or instead of EAX, ECX, or EDX use EBX, ESI, or EDI as these registers will be preserved across the call.