问题
Currently learning assembly language using Masm. This is for an assignment in my class.
I must do certain calculations using 32-bit registers (EAX and EBX). I have to handle BYTE, WORD, and DWORD variables. Not really complicated. I do not really understand why am I getting so many errors when assembling the current code:
INCLUDE Irvine32.inc
.data
; (declare variables)
bNum01 BYTE 64
bNum02 BYTE 32
bNum03 BYTE 16
bSum BYTE ?
bDiff BYTE ?
bResult BYTE ?
wNum01 WORD 64
wNum02 WORD 32
wNum03 WORD 16
wSum WORD ?
wDiff WORD ?
wResult WORD ?
dwNum01 DWORD 64
dwNum02 DWORD 32
dwNum03 DWORD 16
dwSum DWORD ?
dwDiff DWORD ?
dwResult DWORD ?
dwTotal DWORD ?
.code
main PROC
; (insert executable instructions)
movzx eax,bNum01
add eax,bNum02
add eax,bNum03
movzx bSum,eax
movzx eax,bNum02
add eax,bNum03
sub eax,bNum01
movzx bDiff,eax
movzx eax,bSum
add eax,bDiff
movzx bResult,eax
mov esi,OFFSET bSum
mov ecx,LENGTHOF bSum
mov ebx,TYPE bSum
call DumpMem ; call Dump Memory for selected offset value
mov esi,OFFSET bDiff
mov ecx,LENGTHOF bDiff
mov ebx,TYPE bDiff
call DumpMem ; call Dump Memory for selected offset value
mov esi,OFFSET bResult
mov ecx,LENGTHOF bResult
mov ebx,TYPE bResult
call DumpMem ; call Dump Memory for selected offset value
; WORD main processing
movzx ebx,wNum01
add ebx,wNum02
add ebx,wNum03
movzx wSum,ebx
movzx ebx,wNum02
add ebx,wNum03
sub ebx,wNum01
movzx wDiff,ebx
movzx ebx,wSum
add ebx,wDiff
movzx wResult,ebx
mov esi,OFFSET wSum
mov ecx,LENGTHOF wSum
mov ebx,TYPE wSum
call DumpMem ; call Dump Memory for selected offset value
mov esi,OFFSET wDiff
mov ecx,LENGTHOF wDiff
mov ebx,TYPE wDiff
call DumpMem ; call Dump Memory for selected offset value
mov esi,OFFSET wResult
mov ecx,LENGTHOF wResult
mov ebx,TYPE wResult
call DumpMem ; call Dump Memory for selected offset value
; DWORD main processing
mov eax,0
mov eax,dwNum01
add eax,dwNum02
add eax,dwNum03
mov dwSum,eax
mov eax,0
mov eax,dwNum02
add eax,dwNum03
sub eax,dwNum01
mov dwDiff,eax
mov eax,0
mov eax,dwSum
add eax,dwDiff
mov dwResult,eax
mov esi,OFFSET dwSum
mov ecx,LENGTHOF dwSum
mov ebx,TYPE dwSum
call DumpMem ; call Dump Memory for selected offset value
mov esi,OFFSET dwDiff
mov ecx,LENGTHOF dwDiff
mov ebx,TYPE dwDiff
call DumpMem ; call Dump Memory for selected offset value
mov esi,OFFSET dwResult
mov ecx,LENGTHOF dwResult
mov ebx,TYPE dwResult
call DumpMem ; call Dump Memory for selected offset value
; Main side for Dwtotal
mov eax,0
movzx eax,bSum
add ebx,wSum
add eax,dwSum
mov dwTotal,eax
mov esi,OFFSET dwTotal
mov ecx,LENGTHOF dwTotal
mov ebx,TYPE dwTotal
call DumpMem ; call Dump Memory for selected offset value
exit
main ENDP
; (additional procedures)
END main
The errors start in the line where I add bNum02 to the EAX register after using MOVZX to move the value of Bnum01 to the EAX register. It just says that it is an "invalid instruction operands". Which, from what I understand, it is telling me "That's not how it works". I try to researched but nothing really made sense.
Thank you to anyone that takes the time to explain.
回答1:
The operands of an instruction must have the same type resp. size. You cannot move a BYTE
variable to a DWORD
register. The special instructions for that purpose are MOVZX
and MOVSX
which convert the smaller operand appropriate. Storing in this way (movzx bSum,eax
) is not possible, but you can use the least significant byte of EAX
- the AL
register:
movzx eax,bNum01
movzx ebx,bNum02
add eax, ebx
movzx ebx,bNum03
add eax, ebx
mov bSum, al
The least significant WORD of EBX
is called BX
register (EAX->AX, EBX->BX, ECX->CX, EDX->DX):
mov wSum, bx
来源:https://stackoverflow.com/questions/43883522/assembly-move-with-zero-fill-for-different-data-sizes