【发布时间】:2021-11-21 14:29:06
【问题描述】:
我正在尝试用汇编 x86-64 为英特尔 64 位处理器编写程序。该程序应使用 gas(GNU 汇编器)编译并在 Linux 上运行。问题是编写一个名为 lowercase 的程序,它接受输入字符串并打印该字符串的小写字母。 应该这样编译:
$> echo "STRING" | ./lowercase
string
$>
我编写了程序,但问题是它会无限打印空格。谁能帮我理解为什么下面的代码会这样?
.section .bss
.comm buf, 1
.section .text
.globl _start
_start:
mov $65, %bh
mov $97, %ch
mov $0, %dh
Loop:
mov $0, %rax # syscall number for read
mov $0, %rdi # where to read from: stdin
mov $buf, %rsi # buffer adr
mov $1, %rdx # length of the buffer in bytes
syscall
cmpb %dh, buf # if read returns 0 (EOF) or less then 0 exit
jle Exit
cmpb %bh, buf # if the character is less than 65 (Char A) print it
jl Write
cmpb %ch, buf # if the charcter is less than 97 make it lowercase
jl ToLowercase
Write:
mov $1, %rax # system call for write
mov $1, %rdi # file handle for stdout
mov $buf, %rsi # address of string to output
mov $1, %rdx # number of bytes
syscall
jmp Loop
ToLowercase:
addb $32, buf # Make the character lowercase
jmp Write # And go back to output it
Exit:
mov $60, %rax # system call for exit
movb $0, %dil # return code
syscall
【问题讨论】:
-
您是否尝试过单步调试并检查每条指令是否符合您的预期?如果没有,这就是去这里的方式。如果您有,请在此处包含有关哪些指令未达到您的期望的信息。调试是程序员的一项基本技能,尤其是对于汇编而言。