首页 > 解决方案 > Irvine ch10. Three-Operand Instructions using Macro in MASM

问题描述

I'm trying to make 4 macros and trying to calculate 4 operations with it. But Syntax error occurs when I assemble the code

I'm using VS2017, and I wrote the code based on the instructions of the book.

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

Error Message I got is like below. A lot of syntax errors occurs when I debug my program.

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

标签: assemblymasm

解决方案


您错误地假设add,sub指令仅采用一个参数。这仅对mul,imul和是正确dividiv。因此,将您的代码更改为

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

指令放在最后,如有必要,将 amain ENDS作为最后一行。


推荐阅读