jen*_*ena 2 assembly cpu-registers
我写了一个小的汇编代码段(Gas,32位),它接受一个命令行参数,计算它的字符并打印字符串,如果它有一定的长度(仅用于调试目的).我对汇编比较陌生,所以我很确定这里有一些我想念的东西,因为当我将字符串存储在eax中时,我会得到不同的行为,例如ecx,edx或esi.
这是片段.当用eax替换esi时,无论字符串有多长,循环都只输入两次,因此计数器(ebx)始终为1.使用esi或其他寄存器,一切似乎都能正常工作.
.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
Run Code Online (Sandbox Code Playgroud)
谁能给我一个关于我做错了什么/误解的提示?
电贺
该al寄存器是真正的最低8位eax寄存器.因此,指令movb (%eax), %al会破坏最低的8位eax,即指针.
作为一般建议,学习使用调试器逐步执行代码并找出计算机执行与您期望的不同的操作.
编辑:在发布的代码中有一些微不足道的语法错误(例如ebc代替ebx),但我假设一些复制错误,因为你说否则它的工作原理.
| 归档时间: |
|
| 查看次数: |
486 次 |
| 最近记录: |