如何创建仅使用寄存器来表示标识符的汇编语言?
为了澄清,问题:
salary = reghours * 3 + overtimehours * 2 - benifit * 3
它不能使用变量(.data
)...
这是我到目前为止创建的:
mov eax,3
mov ebx,2
mul ebx
call dumpRegs
mov ecx,2
mov ebx,2
mul ebx
call dumpRegs
mov edx,3
mov ebx,5
mul ebx
call dumpRegs
好的,两个上述指令计算是否正确,edx寄存器继续为零?
我的问题是,如何计算包含 reg + reg - reg 以及 reg 总和的工资计算?
To clarify, the question:
salary = reghours * 3 + overtimehours * 2 - benifit * 3
It can not use variables (.data
)...
This is what I created so far:
mov eax,3
mov ebx,2
mul ebx
call dumpRegs
mov ecx,2
mov ebx,2
mul ebx
call dumpRegs
mov edx,3
mov ebx,5
mul ebx
call dumpRegs
okay, the two above instruction computation are correct by edx register continue to be zero?
my question is, how to get the calculation for salary that will include the reg + reg - reg with total sum of a reg?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅 3-746 Vol. 2A 我们看到您使用的
mul
指令是:换句话说,
eax
乘以给定的操作数 (ebx
在本例中)给出存储在edx:eax
中的 64 位结果,即 32 个最高有效位存储在edx
中,32 个最低有效位存储在eax
。所以:
将
edx:eax
设置为6 = 00000000:00000006
,这就是edx
被清除的原因。保留寄存器的标准方法是将它们压入堆栈(
push edx
),然后恢复它们(pop edx
)。由于您的程序中没有那么多变量,您还可以使用有符号乘法指令之一,该指令不需要操作数和结果位于edx
和eax
。例如,
它允许您执行
imul eax, eax, 3
将eax
乘以 3。将此与 @Pete Wilson 的回答来实际计算该值。
Referring to 3-746 Vol. 2A we see that the
mul
instruction you're using is:In other words
eax
is multiplied by the given operand (ebx
in this case) to give a 64-bit result that's stored inedx:eax
, i.e. the 32 most significant bits are stored inedx
and the 32 least ineax
.So:
Will set
edx:eax
to6 = 00000000:00000006
, which is whyedx
is cleared.The standard way of preserving registers is to push them on the stack (
push edx
) and later restore them (pop edx
). Since you don't have that many variables in your program you could also use one of the signed multiplication instructions that don't require the operands and result to be inedx
andeax
.e.g.
Which allows you to do
imul eax, eax, 3
to multiplyeax
by 3 in place.Combine this with @Pete Wilson's answer to actually calculate the value.
第 1 步:按照执行第 2 步时有意义的顺序执行三个较高优先级运算中的每一个:
在每次计算之后,将中间结果推送到堆栈中。
第 2 步:完成所有三个计算后,通过将三个中间结果从堆栈中弹出来开始计算最终结果。
瞧:没有使用 .data 空间。这个作业练习可能是为了让您理解和使用堆栈。顺便说一句,你没有检查作业标签。
您也可以通过将中间结果保存在寄存器中来解决此问题,但这有点困难:即,它要求您仔细考虑要保存哪些中间结果以及何时计算和保存它们。
Step 1: Do each of the three higher-precedence operations in an order that will make sense when you get to step 2:
After each of these calculations, push the intermediate result onto the stack.
Step 2: When all three calculations are done, start calculating the final result by popping each of the three intermediate results off the stack.
Voila: no .data space used. This homework exercise might be meant to get you to understand and use the stack. By the way, you didn't check the homework tag.
You could also do this problem by saving intermediate results in registers, but that's a little more difficult: i.e., it requires that you think carefully about what intermediate results to save and when to calculate and save them.