0

So im just starting to learn assembly and I got this exercise to write a code that takes an input from user of 3 digit number, adds 999 and prints the result of the 4 digit number. (Example: User inputs 100, code does math adds 999, prints 1099)

Here is what I came up with:

section .data
    numToAdd db "999",0x00

    userPrompt db "Please enter a 3 digit number (000-999):", 0x0a, 0x00
    len_userPrompt equ $-userPrompt

section .bss
    userNum resb 4      ; user input: (3 digit num + \n)
    resultNum resb 4    ; Result with a 4 digit number

section .text
    global main
    main:
        mov eax, 0x04
        mov ebx, 0x01
        mov ecx, userPrompt
        mov edx, len_userPrompt
        int 0x80

        mov eax, 0x03
        mov ebx, 0x00
        mov ecx, userNum
        mov edx, 4
        int 0x80

        mov edx, 0x02   ; Digit counter
        mov ecx, 0x03   ; No. of times to loop 3 digits - 3 looops
        clc
        loop_here:
            mov al, [numToAdd+edx]  ; Moves: 99|9 -> AL
            adc al, [userNum+edx]   ; Adds: AL(9) + 3rd number\n 
            aaa     ; Adjust after ADC ( It just adds 96h ? )
            pushf
            add al, 0x30
            popf
            mov [resultNum+edx], al ; Moves the result to the corresponding index in resultNum
            dec edx
            loop loop_here

        mov eax, 0x04
        mov ebx, 0x01
        mov ecx, resultNum
        mov edx, 0x04
        int 0x80

        mov eax, 0x01
        int 0x80 

The problem persists in this line mov [resultNum+edx], al ; Moves the result to the corresponding index in resultNum Since it will grab only AL which is 2 Bytes and put it in the resultNum var. meaning if I add 9+1, which is 39h+31h=6ch AAA will return 102 than I add 30h to al will return 132. The command above will only take '32' and put in the resultNum memory location and not the full representation of the real result (which is 10)

To summarize what is the problem here.

TL;DR: I need to add 999 decimal to any 3 digit number from user input and print the real math result to stdout.

  • 1
    _"it will grab only AL which is 2 Bytes"_ `al` is one byte. – Michael Oct 30 '20 at 18:45
  • Normally you'd convert string->integer, add, convert back. [NASM Assembly convert input to integer?](https://stackoverflow.com/q/19309749) / [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894). Using BCD / base-10 extended precision is something you'd usually only do for novelty. – Peter Cordes Oct 30 '20 at 23:28
  • @Michael ya my bad sorry – Robert Tiger Nov 01 '20 at 08:33
  • Thanks i will look at it @PeterCordes – Robert Tiger Nov 01 '20 at 08:33

0 Answers0