尔湾ch10。在MASM中使用宏的三操作数指令

时间:2019-06-03 16:52:33

标签: assembly masm

我正在尝试制作4个宏,并尝试使用它计算4个操作。但是当我汇编代码时会出现语法错误

我正在使用VS2017,并且根据本书的说明编写了代码。

add3 MACRO destination, source1, source2
mov eax, source1
add source2
mov destination, eax
ENDM

sub3 MACRO destination, source1, source2
mov eax, source1
sub source2
mov destination, eax
ENDM

mul3 MACRO destination, source1, source2
mov eax, source1
mul source2
mov destination, eax
ENDM

div3 MACRO destination, source1, source2
mov eax, source1
div source2
mov destination, source1
ENDM

.data
temp DWORD 0
x   DWORD ?
y   DWORD ?
z   DWORD ?

.code
main PROC

; Ex1. x = (w + y) * z
mov x, ?
mov y, 1
mov z, 2
mov w, 3
add3 temp, w, y     ; temp = w + y
mul3 x, temp, z     ; x = temp * z
mov eax, x
call    WriteInt
call    Crlf

我收到的错误消息如下。调试程序时会发生很多语法错误。

13_4.asm(45): error A2008: syntax error : in instruction
1>13_4.asm(56): error A2008: syntax error : ,
1>13_4.asm(57): error A2008: syntax error : ,
1>13_4.asm(67): error A2008: syntax error : ,
1>13_4.asm(68): error A2008: syntax error : ,
1>13_4.asm(78): error A2008: syntax error : ,
1>13_4.asm(79): error A2008: syntax error : ,
1>13_4.asm(41): error A2009: syntax error in expression
1>13_4.asm(44): error A2006: undefined symbol : w
1>13_4.asm(45): error A2006: undefined symbol : w
1>13_4.asm(52): error A2009: syntax error in expression
1>13_4.asm(55): error A2006: undefined symbol : w
1>13_4.asm(58): error A2006: undefined symbol : w
1>13_4.asm(65): error A2009: syntax error in expression
1>13_4.asm(66): error A2006: undefined symbol : w
1>13_4.asm(75): error A2009: syntax error in expression
1>13_4.asm(77): error A2006: undefined symbol : w

1 个答案:

答案 0 :(得分:1)

您错误地假设addsub指令仅采用一个参数。这仅对mulimuldividiv正确。因此,将您的代码更改为

add3 MACRO destination, source1, source2
  mov eax, source1
  add eax, source2
  mov destination, eax
ENDM

sub3 MACRO destination, source1, source2
  mov eax, source1
  sub eax, source2
  mov destination, eax
ENDM

mul3 MACRO destination, source1, source2
  mov eax, source1
  mul source2
  mov destination, eax  ; This is only the low 32-bit result of high(EDX):low(EAX)
ENDM

div3 MACRO destination, source1, source2
  xor edx, edx          ; Clear upper half of input EDX:EAX
  mov eax, source1
  div source2
  mov destination, eax
ENDM

这些更改应修复代码的一些主要错误。
现在,关于您的main代码:

; Ex1. x = (w + y) * z
mov x, ?            ; YOU CANNOT SET a register to an unknown value - it already is. Remove this line instead.
mov y, 1            ; OK
mov z, 2            ; OK
mov w, 3            ; OK
add3 temp, w, y     ; temp = w + y
mul3 x, temp, z     ; x = temp * z - Here 'x' is replaced with a value
mov eax, x          ; Set the parameter EAX to the value 'x'
call    WriteInt    ; Write the value in EAX and...
call    Crlf        ; ...proceed to the next line

我尚未测试此代码,但是它应该产生正确的值8

还要添加一个

main ENDP
在末尾加上

指令,并在最后一行加上main ENDS