使用汇编语言问题......以及如何换行?

时间:2012-02-14 16:23:22

标签: linux assembly

我开始学习汇编语言。我需要做的计划非常简单。 1 - 读取一个4位数字(可以是负数) 2 - 输出输入数字的编号 3 - 计算/输出输入数字的一半 4 - 计算/输出输入数字的两倍

所以......这是代码......首先是数据部分

section .data ; Data segment
    msgPrompt       db 'Please enter a number ' ; Ask the user to enter a number
    lenMsgPrompt    equ $-msgPrompt          ; The length of the message
    msgNumber       db 'The entered number is '
    lenMsgNumber    equ $-msgNumber                 
    msgHalf         db 'The half of the entered number is '
    lenMsgHalf      equ $-msgHalf
    msgDouble       db 'The double of the entered numbered is '
    lenMsgDouble    equ $-msgDouble

然后是未初始化的数据(用于用户输入)

section .bss        ; Uninitialized data
    userNumber    resb 5
    halfNumber    resb 5
    doubleNumber  resb 5

然后......代码段......

    section .text       ; Code Segment

       global _start
       _start:

       ;Prompt the user
       mov eax, 4
       mov ebx, 1
       mov ecx, msgPrompt
       mov edx, lenMsgPrompt
       int 80h

       ; Read and store the user input
       mov eax, 3
       mov ebx, 2
       mov ecx, userNumber  
       mov edx, 5       ; 5 bytes (numeric, 1 for sign) of that information
       int 80h

       ; Output the message 'The entered number is '
       mov eax, 4
       mov ebx, 1
       mov ecx, msgNumber
       mov edx, lenMsgNumber
       int 80h  

       ; Output the number entered
       mov eax, 4
       mov ebx, 1
       mov ecx, userNumber
       mov edx, 5
       int 80h  

       ; Output the message 'The half of the entered number is '
       mov eax, 4   
       mov ebx, 1
       mov ecx, msgHalf
       mov edx, lenMsgHalf
       int 80h

       ; Calculate the half of the number entered
       mov eax, [userNumber]
       shr eax, 1       ; Shift to the right by one bit
       mov [halfNumber], eax

       ; Output half of the number entered
       mov eax, 4
       mov ebx, 1
       mov ecx, halfNumber
       mov edx, 5
       int 80h

       ; Output the message 'The double of the entered numbered is '
       mov eax, 4
       mov ebx, 1
       mov ecx, msgDouble
       mov edx, lenMsgDouble
       int 80h

       ; Calculate the double of the number entered
       mov ebx, [userNumber]
       shl ebx, 1       ; Shift the the left by one bit
       mov [doubleNumber], ebx

       ; Output double of the number entered
       mov eax, 4
       mov ebx, 1
       mov ecx, doubleNumber
       mov edx, 5
       int 80h

       ; Leave program
       mov eax, 1
       mov ebx, 0
       int 0x80

这是最终结果...... Here(我还没有足够的声望发布图片......)

正如你所看到的那样,有问题...另外,我希望在“数字的一半是xxxxx”之后有一个新的行。

谢谢

2 个答案:

答案 0 :(得分:1)

看起来你正在对数字的ASCII代码进行算术运算,而不是数值。

答案 1 :(得分:0)

您缺少EOL代码(13,10)?

msgHalf         db 'The half of the entered number is ', 13, 10