EAX的不同的行为相比其他寄存器 [英] Different behaviour of eax compared to other registers

查看:207
本文介绍了EAX的不同的行为相比其他寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小型组装片段(天然气,32位),需要一个命令行参数,计算它的人物和打印字符串,如果它有一定的长度(只用于调试)。我是比较新的组件,所以我pretty肯定有东西,我想念这里是因为我得到不同的行为,当我字符串存储在EAX相比,例如,ECX,EDX或ESI。

I've written a small assembly snippet (Gas, 32 bit) that takes a command-line argument, counts its characters and prints the string if it has a certain length (just for debugging purposes). I'm relatively new to assembly, so I'm pretty sure there is something I miss here because I get different behaviour when I store the string in eax compared to, for instance, ecx, edx or esi.

下面是片段。当你与EAX更换ESI,循环只输入两次无论串有多长,因此计数器(EBX)总是1. ESI或其他寄存器,一切似乎都正常工作。

Here is the snippet. When you replace esi with eax, the loop is entered only twice no matter how long the string is, hence the counter (ebx) is always 1. With esi or other registers, everything seems to work fine.

.section .text
.globl _start
_start:
    movl    %esp, %ebp
    movl    0(%ebp), %eax   # get argc
    cmpl    $2, %eax        # ensure argc == 2
    jne     _exit           
    movl    8(%ebp), %eax   # get argv[1]
    movl    $0, %ebx        # set counter to 0
_begin_loop:
    movb    (%eax), %al     # load a character into %al
    cmpb    $0, %al         # see if \0 is reached
    je      _end_loop       # exit loop if at end of string
    incl    %ebc            # increment counter
    incl    %eax            # advance string
    jmp     _begin_loop
_end_loop:
    cmpl    $6, %ebx        # print the string if it's six characters long
    jne     _exit
    movl    $4, %eax        # prepare for output
    movl    $1, %ebx
    movl    8(%ebp), %ecx)
    movl    $6, %edx
    int     0x80
_exit:
    movl    $1, %eax
    movl    $0, %ebx
    int     0x80

任何人可以给我什么,我做错了/误解暗示?

Can anybody give me a hint about what I'm doing wrong/misunderstanding?

问候

推荐答案

寄存器是真正的的最低8位EAX 注册。那么,指令 MOVB(%EAX),%人破坏 EAX 的最低8位,这是你的指针

The al register is really the lowest 8 bits of the eax register. So, the instruction movb (%eax), %al destroys the lowest 8 bits of eax, that is your pointer.

作为一般的建议,学会使用调试器与预期的逐步执行code和那个地方的计算机做不同的东西。

As a general advice, learn to use a debugger to step through your code and spot where the computer does something different from what you expect.

编辑:有在贴code(一些琐碎的语法错误,如 EBC 而不是 EBX ),但我认为有些复制错误,因为你说的,否则它的工作原理。

there are some trivial syntax errors in the posted code (such as ebc instead of ebx), but I assume some copying error since you say otherwise it works.

这篇关于EAX的不同的行为相比其他寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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