簡體   English   中英

程序集x86,問候程序

[英]Assembly x86 , greeting program

我嘗試使用程序集x86編寫簡單的問候語程序,該程序集采用用戶名並打印出“ Hello [userName]”,問題是在打印問候語msg時用戶名的第一個字符加倍,例如:

輸入:

Black Knight 

輸出:

Hello BBlack Knight 

這是我的代碼

global _start 

section .data 
    msg1        db "Hello "
    user_input  times 20 db 0 


section .bss 

section .text 

_start : 

; read 
mov eax , 3 
mov ebx , 0
mov ecx , user_input
mov edx , 20
int 0x80

; write 
mov eax , 4
mov ebx , 1
mov ecx , msg1
mov edx , 7
int 0x80

mov eax , 4 
mov ebx , 1
mov ecx , user_input
mov edx , 20
int 0x80

; exit 
mov eax , 1
mov ebx , 0
int 0x80

發生這種情況是由於以下代碼:

; write 
mov eax , 4
mov ebx , 1
mov ecx , msg1
mov edx , 7
int 0x80

您在這里告訴寫指令,要打印的字符串實際上是6個字節,長度為7個字節。

為什么B加倍? 因為在內存中,輸入的名稱出現在msg1 緊隨其后的字節開始。

您可以通過僅打印6個字符(Hello +空格)或在msg值的末尾添加空終止符來解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM