【问题标题】:Cannot print a number in NASM无法在 NASM 中打印数字
【发布时间】:2021-12-20 10:59:05
【问题描述】:

我刚开始学习汇编程序(出于爱好),我制作了这个小程序,它可以从用户那里获取一个数字并将其打印出来:

section .data:
        message: db "please enter a number: "           ;define message to "please enter a number"
        message_length equ $-message                    ;define message_length to $-message the lenght of the message
        message_done: db "you have entered the number " ;define message_done to "you have entered the number "
        message_done_length equ $-message               ;define message_done_length to the $-message_done the length of message_done
section .bss:
        num resb 5              ;num will store the input the user will give

section .text:
        global _start
_start:
        mov eax, 4              ;set the next syscall to write
        mov ebx, 1              ;set the fd to stdout
        mov ecx, message        ;set the output to message
        mov edx, message_length ;set edx to the length of the message
        int 0x80                ;syscall

        mov eax,3               ;set the next syscall to read
        mov ebx, 2              ;set the fd to stdout
        mov ecx, num            ;set the output to be num
        mov edx, 5              ;set edx to the length of the num
        int 0x80                ;syscall

        mov eax, 4                      ;set the syscall to write
        mov ebx, 1                      ;set the fd to stout
        mov ecx, message_done           ;set the output to the message
        mov edx, message_done_length    ;set edx to the message_done_length
        int 0x80                        ;syscall

        mov eax, 4              ;set the syscall to write
        mov ebx ,1              ;set the fd to stdout
        mov ecx, num            ;set the output to num
        mov edx, 5              ;the length of the message
        int 0x80                ;syscall

        mov eax, 1              ;set the syscall to exit
        mov ebx, 0              ;retrun 0 for sucsess
        int 0x80                ; syscall

当我运行它并输入一个数字并按回车键时,我得到了这个:

please enter a number: you have entered the number ����

我做错了吗?

【问题讨论】:

    标签: nasm stdout write


    【解决方案1】:

    三个错误:

    section .data:
    

    这组合成一个名为.data: 的部分,它不同于.data。放下冒号。部分指令不是标签。同样的问题出现在所有其他 section 指令中。

    message_done: db "you have entered the number "
    message_done_length equ $-message
    

    应该是$-message_done。就目前而言,您正在写入太多字节。这可能是您在留言后看到垃圾的原因。

            mov eax,3               ;set the next syscall to read
            mov ebx, 2              ;set the fd to stdout
            mov ecx, num            ;set the output to be num
            mov edx, 5              ;set edx to the length of the num
            int 0x80                ;syscall
    

    您希望 fd 是标准输入 (0),您的注释说标准输出,而文件描述符 2 实际上是标准错误。让它mov ebx, 0。当您从终端运行程序时,它可能会按原样工作,因为文件描述符 0、1 和 2 在终端上都是打开的并且都是可读写的,但如果您使用输入重定向,它会出现异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2013-09-19
      • 2016-01-07
      • 1970-01-01
      • 2014-02-21
      • 2016-01-21
      • 2015-09-17
      相关资源
      最近更新 更多