What am I doing wrong? (Simple Assembly Loop)
It won't let me post the picture. Btw, Someone from Reddit.programming sent me over here. So thanks!
TITLE MASM Template
; Description
;
; Revision date:
INCLUDE Irv开发者_Python百科ine32.inc
.data
myArray BYTE 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
.code
main PROC
call Clrscr
mov esi, OFFSET myArray
mov ecx, LENGTHOF myArray
mov eax, 0
L1:
add eax, [esi]
inc esi
loop L1
call WriteInt
exit
main ENDP
END main
Results in:
-334881242
You have to cast the value to a byte pointer instead of 32 bit pointer.
Change
add eax, [esi]
to
add eax, byte ptr [esi]
You're trying to get a DWORD from a byte table, so masm gives you an error.Forcing byte size here using BYTE PTR is gonna get you a masm error (invalid operand or something alike), because you can't directly add a byte to a DWORD.
But there are still several ways to do what you want. Here is one that costs an extra register (EDX):
(...)
mov edx, 0 ; We want the upper 3 bytes to zero.
mov eax, 0
L1:
mov dl, [esi] ; Inject one byte from the table,
add eax, edx ; upper bytes still zero, so EDX has your byte value.
(...)
INCLUDE Irvine32.inc .data myArray BYTE 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 .code main PROC call Clrscr mov esi, OFFSET myArray mov ecx, LENGTHOF myArray xor eax, eax L1: add eax, byte ptr [esi] inc esi loop L1 call WriteInt exit main ENDP END main
I'm assuming, WriteInt
is taking a parameter in EAX to print out the results, since the code failed because you were using a 32bit pointer to the offset into the data within the ESI
register, change that to a byte ptr
in order to get the proper 8 bits (a byte). Also, use of XOR
would be faster than a MOV
instruction for the EAX
register, then the code should work...
Hope this helps, Best regards, Tom.
Okay here's the thing:
My guess is that WriteInt expects a 32-bit signed value in EAX. Therefore you could do:
movzx eax, BYTE PTR [esi]
inc esi
call WriteInt
loop L1
-- or --
dec ecx
jnz L1
Or if you are sure that WriteInt do not touch EAX you can do:
xor eax,eax ; clear EAX
L1:
lodsb ; loads a byte into AL and increments ESI for you
call WriteInt
loop L1
精彩评论