Search code examples
assemblymasm

How can i make 10000,1000,100 from 10 the easiest way


i am looking for a solution for the question abowe. I need to generate 100,1000 and 10000(decimal numbers). Because the whole exercise is to caluclate:

10000*X+1000*Y+100*Y+10*V+1*C

I know i can do it by the mul command,but in that case i have to work a lot with stack.

I was thinking some kind of Shifting, but don't know how to do it with decimal numbers.

With binary numbers

mov al,2h;mov al,10b;
shl al,1

Preferebla is masm enviroment.Thank you for your help

Update1: I dont want to use mul nor imul(less then 32 bit number) I have a number as string(db) lets say 24575,i have hier the pseudo code,not finished

    mov cx,5
    Calc:
mov di,offset first
add di,2;so we are on the adress of "2"


;ASCI number is 2 and i want to get 20000
    mov al,10d

    ;than ASCI number is 4 and i want to get 4000

    ;than ASCI number is 5 and i want to get 500

    ;than ASCI number is 7 and i want to get 70

    ;than ASCI number is 5 and i want to get 5


inc di
    loop Calc

first:db"A$"
number :db"24575$"

Solution

  • To calculate 10000*X+1000*Y+100*Z+10*V+1*C use the transformation 10*(10*(10*(10*X+Y)+Z)+V)+C. This way you only need to multiply by 10 in every iteration. As I have written in my comment, you can avoid a mul by using 10=8+2. As such your code may look something like this:

        mov cx, 5               ; number of digits
        mov di, offset number   ; pointer to digits
        xor ax, ax              ; zero result
    next:
        mov dx, ax              ; save a copy of current result
        shl ax, 3               ; multiply by 8
        add ax, dx              ; 9
        add ax, dx              ; 10
        movzx dx, byte ptr [di] ; fetch digit
        sub dx, '0'             ; convert from ascii
        add ax, dx              ; add to sum
        inc di                  ; next digit
        loop next