简体   繁体   English

程序集x86,问候程序

[英]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 : 我尝试使用程序集x86编写简单的问候语程序,该程序集采用用户名并打印出“ Hello [userName]”,问题是在打印问候语msg时用户名的第一个字符加倍,例如:

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. 您在这里告诉写指令,要打印的字符串实际上是6个字节,长度为7个字节。

Why the doubled B? 为什么B加倍? Because in memory, the inputted name appears beginning in the byte immediately following msg1 . 因为在内存中,输入的名称出现在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. 您可以通过仅打印6个字符(Hello +空格)或在msg值的末尾添加空终止符来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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