0

I've spent the whole thinking about how to solve this and I just cannot seem to make it work although I think everything should be fine.

This is supposed to be a calculator, so when user enters a number, the digits entered are stored in a 4 byte array called num1 in the order they were typed. Initially num1 array it's full of 0's, so let's suppose a user types 9764, then array will be [9,7,6,4] but if user types, say 89, array will be [8,9,0,0].

The program uses a byte counter beginning in 0 to store the number of times a user typed a number and to keep track of the position it will be placed. As a usual array, position 0 goes first, then counter increases by one with next number entered and so on up to 4.

The number entered by the user should be stored in a word called num1h beginning with a 0 value as well.

So my approach was the following... let's try with 875

num1 = [8,7,5,0]

Counter = 3

Counter - 1 = 2
num1[2] = 5
5 x 1 = 5
num1h + 5 = 5 

Counter - 1 = 1
num1[1] = 7
7 x 10 = 70
num1h + 70 = 75 

Counter - 1 = 0
num1[0] = 8
8 x 100 = 800
num1h + 800 = 875

So this is the code I'm using, but when I enter 875, I receive 0005 as a result

NUM1P proc
        push ax
        push bx
        push cx
        push dx

        mov di, counter
        mov ax,0
        mov bx,1
        
    power_base: 
        dec di
        mov al, [num1+di]
        mul bx
        add num1h,ax
        mov ax,bx
        mov bx, 10
        mul bx
        mov bx,ax
        mov ax,0
        cmp di,0
        jl exitn1p

    exitn1p:

    pop dx
    pop cx
    pop bx
    pop ax

    ret
    endp     

Result

Printing function works perfectly fine, i've tested it implementing immediate values and it just prints 0's to the left, example 875 -> 0875, but that's totally fine. Instead when I try to transfer num1h to it, this is what happens so the problem should be in my code.

Any suggestions?

I tried some other code like this, but definately not the best solution because it takes for granted every digit in the first position it's in the thousands place and so on...

TESTPROC proc

    push ax
    push bx

    mov ax,0
    mov bx,0

    movzx ax, [num1]
    mov bx, 1000
    mul bx
    add num1h, ax

    movzx ax, [num1+1]
    mov bx, 100
    mul bx
    add num1h, ax

    movzx ax, [num1+2]
    mov bx, 10
    mul bx
    add num1h, ax

    movzx ax, [num1+3]
    add num1h, ax

    pop bx
    pop ax

    ret
    endp
Adolf RJ
  • 19
  • 2
  • 1
    You never loop back to `power_base` so you only do 1 digit. Instead of `jl exitn1p` try `jge power_base`. Use your debugger/simulator to single step the code to see what it is doing. – Jester Jun 21 '23 at 13:58
  • 1
    If you're not familiar with single step debugging, now is a good time to learn it. You can debug assembly similar to debugging other code, single step and observe the effects of the last line on the program state — if something differs from what you expect, then that's a good place to start looking. Use small inputs to test and debug at first, then when they're working start using larger inputs. – Erik Eidt Jun 21 '23 at 14:20
  • Just corrected it and it gives me back 61803. I will try it in another file, I cannot really see whats going on with the calc because its a ui :( – Adolf RJ Jun 21 '23 at 14:20
  • 1
    Thanks for your help guys, I already saw what was happening with the debugger as you suggested. :) – Adolf RJ Jun 21 '23 at 14:42

0 Answers0