为什么寄存器 ax 中的内存整数总和是正确的,而寄存器 eax 则不是? [英] Why is correct sum of memory integers in register ax but not register eax?

查看:48
本文介绍了为什么寄存器 ax 中的内存整数总和是正确的,而寄存器 eax 则不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这个项目——我正在帮助的一个学生项目——

Given this program -- a student's program whom I am helping --

        global  _start

        section .text
_start:
        mov    ebx, people 
        mov    eax, [ebx + 2]
        add    eax, [ebx + 4]
        add    eax, [ebx + 6]
        add    eax, [ebx + 8]
        mov    ebx, [constant]
        div    bx

section .data

average     dw 0
constant    dw 5
people      dw 0, 10, 25, 55, 125

在将它从 Visual Studio 移植到 Linux 机器以找出问题所在时,我遇到了一些问题:

While porting this from Visual Studio to a Linux machine to figure out what the problem was, I ran into some questions:

1) 为什么print $ax 下gdm 显示sum 为255,而print $eax 下发时却出现大数?那是因为我们添加了单词值而不是长字值吗?

1) Why does gdm display the sum 255 when print $ax issued, but a large number appears when print $eax command issued? Is that because we have added word values instead of longword values?

我确实尝试添加到 ax 而不是 eax,并得到了相同的结果.当我尝试将初始值移动到 ax 时,我收到了搬迁投诉.这就是我使用 eax 的原因.

I did try to add into ax, rather than eax, and got the same results. I got a relocation complaint when I tried to move the initial value into ax. That's why I used eax.

2) 为什么div bx 的商数是43,但是如果div ebx 使用,我得到了错误的答案?

2) Why is the quotient 43 when div bx used, but if div ebx used, I get the wrong answer?

顺便说一句,我相信我找到了最初的问题,即整数溢出.第 10 行 -- mov ebx, [constant] -- 原本是 mov ebx,constant,这并没有导致将 5 移动到 bx 中.

As an aside, I believe I found the original problem, which was an integer overflow. Line 10 -- mov ebx, [constant] -- was originally mov ebx,constant, which did not result in moving 5 into bx.

推荐答案

一些问题

  1. 所有数据都定义为word,但代码将其视为dword.

在除法之前,您需要通过DXEDX 来扩大红利.

Prior to the division you need to extent the dividend via DX or EDX.

尽管数组中的第一个值为零,但最好将它也包含在代码中.如果数据发生变化,至少代码仍然有效!

Although the first value in the array is zero, it's probably better to also include it in the code. If ever the data changes, at least the code will remain valid!

<小时>

解决方案

  1. 保留 16 位数据并相应地编程

  1. Keep the data 16-bit and program accordingly

mov    edx, people
mov    ax, [edx]
add    ax, [edx + 2]
add    ax, [edx + 4]
add    ax, [edx + 6]
add    ax, [edx + 8]
xor    dx, dx
div    word ptr [constant]      ;Divide DX:AX by 5
...
constant    dw 5
people      dw 0, 10, 25, 55, 125

  • 将数据设为 32 位并进行相应的编程

  • Make the data 32-bit and program accordingly

    mov    edx, people
    mov    eax, [edx]
    add    eax, [edx + 4]
    add    eax, [edx + 8]
    add    eax, [edx + 12]
    add    eax, [edx + 16]
    xor    edx, edx
    div    dword ptr [constant]     ;Divide EDX:EAX by 5
    ...
    constant    dd 5
    people      dd 0, 10, 25, 55, 125
    

  • <小时>

    看到我如何避免使用 EBX 了吗?

    • 该部门可以直接从内存中使用其分隔符.
    • EDX 寄存器(无论如何都参与除法)也可以同样精细地寻址内存.
    • The division can use its divider straight from memory.
    • The EDX register (that anyway takes part in the division) can also equally fine address memory.

    较少的注册破坏是一件好事!

    Less register clobbering is a good thing!

    这篇关于为什么寄存器 ax 中的内存整数总和是正确的,而寄存器 eax 则不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆