【问题标题】:Assembly x86 , greeting program汇编x86,问候程序
【发布时间】:2017-01-27 11:44:24
【问题描述】:

我尝试使用汇编 x86 编写简单的问候程序,该程序接受用户名并打印出“Hello [userName]” 问题是打印问候信息时用户名的第一个字符加倍,例如:

输入:

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

【问题讨论】:

  • 如果您的 user_input 缓冲区直接在内存中跟随 msg1(如上),您也可以在一次调用中将其输出为一个大字符串,即。 ecx = msg1,edx = 6+20。 (看起来对学生来说是一个很好的例子,在汇编中没有变量,但是计算机内存由连续字节组成,并且您的代码完全负责赋予它从高级语言中已知的所有“结构”含义,标签只是别名内存地址,仅此而已)。因此,通过您定义的内存布局,您将用户输入附加到内存中的 msg1 并且可以在写入期间利用

标签: assembly x86


【解决方案1】:

这是因为这段代码:

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

你在这里告诉写指令要打印的字符串的长度是 7 个字节,而实际上它是 6 个字节。

为什么是双倍的 B?因为在内存中,输入的名字出现在msg1之后的字节立即开始。

您可以通过仅打印 6 个字符(Hello + 空格)或在 msg 值的末尾添加空终止符来解决此问题。

【讨论】:

    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多