简体   繁体   中英

Assembly x86 , greeting program

I tried to write simple greeting program using assembly x86 that takes user name and print out "Hello [userName]" the problem is the first char of user name is doubled when printing the greeting msg , for example :

input :

Black Knight 

output :

Hello BBlack Knight 

and here's my code

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

This is happening because of this code:

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

You are here telling the write instruction that the string to print is 7 bytes in length when it is, in fact, 6.

Why the doubled B? Because in memory, the inputted name appears beginning in the byte immediately following msg1 .

You could solve this by printing only 6 characters (Hello + space) or by adding a null terminator to the end of your msg value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM