Reputation: 55
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
Upvotes: 1
Views: 880
Reputation: 16331
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.
Upvotes: 2