HYOSITIVE
HYOSITIVE

Reputation: 35

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

Upvotes: 0

Views: 156

Answers (1)

zx485
zx485

Reputation: 29022

You are erroneously assuming that the add, sub instructions only take one parameter. This is only correct for mul, imul, div and idiv. So change your code to

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

These changes should fix some major errors of your code.
Now, regarding your main code:

; 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

I haven't tested this code, but it should result in the correct value 8.

Also, add a

main ENDP

instruction at the end and, if necessary, a main ENDS as the last line.

Upvotes: 1

Related Questions