assembly - 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
解决方案
您错误地假设add
,sub
指令仅采用一个参数。这仅对mul
,imul
和是正确div
的idiv
。因此,将您的代码更改为
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
作为最后一行。
推荐阅读
- html - 将图像设置为单选按钮的背景
- docker - 警告:不支持 blkio throttle.read_bps_device。这是什么信息?
- java - 无法在 [Source: (String)"[UsersPajo{website='hildegard.org'
- ios - 为什么 Accelerate vDSP_vsaddD 不像文档中描述的那样工作?
- r - 如何在 R 中使用宏创建对象?
- python - EfficientNet 图像分割
- next.js - NextJS 内容安全策略 (CSP)
- reactjs - 我如何将 useState 值从子级传递给父级
- javascript - 如何修复 err_http_headers_sent
- javascript - 有没有办法使用“询问者”npm 来处理标准输入?