如何创建仅使用寄存器来表示标识符的汇编语言?

发布于 2024-12-11 03:37:11 字数 431 浏览 0 评论 0原文

为了澄清,问题:

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2024-12-18 03:37:11

请参阅 3-746 Vol. 2A 我们看到您使用的 mul 指令是:

F7 /4 MUL r/m32    Unsigned multiply (EDX:EAX ← EAX ∗ r/m32).

换句话说,eax 乘以给定的操作数 (ebx在本例中)给出存储在 edx:eax 中的 64 位结果,即 32 个最高有效位存储在 edx 中,32 个最低有效位存储在eax

所以:

    mov eax,3
    mov ebx,2
    mul ebx

edx:eax 设置为 6 = 00000000:00000006,这就是 edx 被清除的原因。

保留寄存器的标准方法是将它们压入堆栈(push edx),然后恢复它们(pop edx)。由于您的程序中没有那么多变量,您还可以使用有符号乘法指令之一,该指令不需要操作数和结果位于 edxeax

例如,

IMUL r32, r/m32, imm32  doubleword register ← r/m32 ∗ immediate doubleword. 

它允许您执行 imul eax, eax, 3eax 乘以 3。

将此与 @Pete Wilson 的回答来实际计算该值。

Referring to 3-746 Vol. 2A we see that the mul instruction you're using is:

F7 /4 MUL r/m32    Unsigned multiply (EDX:EAX ← EAX ∗ r/m32).

In other words eax is multiplied by the given operand (ebx in this case) to give a 64-bit result that's stored in edx:eax, i.e. the 32 most significant bits are stored in edx and the 32 least in eax.

So:

    mov eax,3
    mov ebx,2
    mul ebx

Will set edx:eax to 6 = 00000000:00000006, which is why edx 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 in edx and eax.

e.g.

IMUL r32, r/m32, imm32  doubleword register ← r/m32 ∗ immediate doubleword. 

Which allows you to do imul eax, eax, 3 to multiply eax by 3 in place.

Combine this with @Pete Wilson's answer to actually calculate the value.

挽袖吟 2024-12-18 03:37:11

第 1 步:按照执行第 2 步时有意义的顺序执行三个较高优先级运算中的每一个:

benifit * 3        // question: why would you do this calculation first?
reghours * 3
overtimehours * 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:

benifit * 3        // question: why would you do this calculation first?
reghours * 3
overtimehours * 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文