multiply two consecutive times in assembly language program

前端 未结 2 914
独厮守ぢ
独厮守ぢ 2021-01-29 14:37

I am using 8086 emulator and DOSBOX and MASM.

I know that when we multiply 8-bit with 8-bit, answer will be of 16-bit.

al*(8-bit)=ax

An

2条回答
  •  盖世英雄少女心
    2021-01-29 15:15

    Imagine you have to do it in the decimal system, e.g. 37 * 8. You would calculate and add two terms: 30 * 8 + 7 * 8. The first term can be transformed to 3 * 8 * base (10).

    37 * 8
    ------
     56    (7*8)
    24     (3*8)
    ======
    296
    

    You see the place of "base" is empty since this value is always 0.

    Now let us change base 10 to base "register":

    DX:AX * CX
    ----------
       DX:AX  (AX*CX)
    DX:AX     (DX*CX) 
    ==========
    XX:XX:XX
    

    You need two multiplications and three words for the result. Furthermore you have to store the result (DX:AX) from the first multiplication because you need the registers for the second multiplication.

    Here's the code for 10! I omit the leftmost word of the result since it isn't needed for 3628800:

    .MODEL SMALL
    .STACK 1000h
    
    .DATA
    Result DD 1                             ; 01 00 00 00 (little endian!)
    
    .CODE
    
    main PROC
        mov ax, @DATA                       ; Initialize DS
        mov ds, ax
    
        mov cx, 2                           ; Start value
    
        L1:
    
        mov ax, WORD PTR Result + 0         ; Low word
        mul cx
        mov di, dx                          ; Store high result
        mov WORD PTR Result + 0, ax         ; Low result won't be changed anymore
    
        mov ax, WORD PTR Result + 2         ; High word
        mul cx
        add ax, di                          ; add low result from last mul to low result here
        mov WORD PTR Result + 2, ax         ; Store it
    
        ; adc dx, 0                         ; this would store the "highest" word of result (13!)
        ; mov WORD PTR Result + 4, dx
    
        add cx, 1
        cmp cx, 10
        jbe L1                              ; While CX <= 10
    
        mov ax, 4C00h
        int 21h
    main ENDP
    
    END main
    

提交回复
热议问题