[英]I want my Assembly Code to takes user input and outputs it along with other text but the output isn't correct
我的代碼有效,但 output 不正確,因為當我輸入名稱時,output 變成了這個; “你的會員名字是什么?歡迎來到俱樂部,Bob,..?!!!!!!!!!!!!!!, 享受派對。” 與“!!!!!!” 在名稱的末尾。 我究竟做錯了什么?
這是我的匯編代碼:
section .data
prompt: db "What is your member name? "
prompt_len: equ $-prompt
greet1: db "Welcome to the club, "
greet1_len: equ $-greet1
greet2: db ", enjoy the party."
greet2_len: equ $-greet2
inputbuffer_len: equ 256
inputbuffer: times inputbuffer_len db '!'
STDIN: equ 0
STDOUT: equ 1
SYS_READ: equ 0
SYS_WRITE: equ 1
SYS_EXIT: equ 60
section .text
global _start
_start:
mov rdx, prompt_len ;output prompt
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, prompt
syscall
mov rax, SYS_READ ;user input here
mov rdi, STDIN
mov rsi, inputbuffer
mov rdx, inputbuffer_len
syscall
mov rdx, greet1_len ; output "Welcome to the club, "
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, greet1
syscall
mov rdx, rax ;output user's inputted name
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, inputbuffer
syscall
mov rdx, greet2_len ; output ", enjoy the party."
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, greet2
syscall
mov rax, SYS_EXIT
mov rdi, 0
syscall
Bob!!!!!!!!!!!!!!!!!!
是Welcome to the club,
. 這並非巧合。 在write(2)
系統調用之后, rax
包含成功寫入的字節數。 (這可能少於手冊頁描述的所需字節數。)
就像David C。Rankin 評論說你需要注意read(2)
的返回值。 成功時, read(2)
返回在rax
中讀取的字節數。 但是,您正在為中間的write(2)
系統調用覆蓋此值。 在某處存儲並調用成功讀取的字節數(例如push
/ pop
)就可以了。
PS:您可以通過重新安排greet_1
之后的緩沖區來節省一個write(2)
系統調用。 然后你可以一次write(2)
rax + greet1_len
字節。 但一次一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.