Проблема с языком ассемблера ... и как вы делаете новую строку? - PullRequest
0 голосов
/ 14 февраля 2012

Я начинаю изучать ассемблер. Программа, которую мне нужно сделать, очень проста. 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

Это окончательный результат ... Здесь (У меня пока недостаточно репутации для публикации изображений ...)

Как видите, есть проблема ... Кроме того, я хотел бы получить новую строку после 'Половина числа - xxxxx'.

Спасибо

Ответы [ 2 ]

1 голос
/ 14 февраля 2012

Похоже, вы выполняете арифметику для кодов ASCII для цифр вместо числового значения.

0 голосов
/ 14 февраля 2012

Вам не хватает кодов EOL (13, 10)?

msgHalf         db 'The half of the entered number is ', 13, 10
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...