我正在嘗試為英特爾 64 位處理器在 Assembly x86-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
uj5u.com熱心網友回復:
這一行:
cmpb %dh, buf # if read returns 0 (EOF) or less then 0 exit
您使用系統呼叫的計數引數修改了 %rdx(因此為 %dh)。此外,系統呼叫沒有保留 %rdx 的合同,因此此檢查無效。
此外,系統呼叫(linux,其他)的回傳值在 %rax 中,因此您正在使用 buf 檢查未定義的值 (%dh)?更像的東西
cmp $1, %rax
jlt Exit
將測驗從讀取回傳。然后你需要看看你是否在'A'..'Z':
...
mov buf, %dl
cmp $'A', %dl
jl write
cmp $'Z', %dl
jle ToLowerCase
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365366.html
下一篇:在螢屏上列印一句話
