装配TASM:将4位数字乘以4位数字

时间:2017-10-20 07:34:35

标签: assembly tasm

我正在尝试使用汇编语言创建计算器程序,但我们需要显示4位数字操作。

在这段代码中,我可以添加,减去和除以4位数字,但是当将4位乘以4位数时,答案是不同的错误

示例:9999x9999 = 37601(这是错的,答案应该是99980001)

以下是乘法代码的一部分:

.model small
.stack 100h

.data
msg1    db 13, 10, 13, 10,"MENU "
        db 10, 13,"1. Add   "
        db 10, 13,"2. Subtract "
        db 10, 13,"3. Multiply "
        db 10, 13,"4. Divide "
        db 10, 13,"5. Exit "
        db 13,10,13,10, "Enter 1st Number : $"
msg2 db 13,10, "Enter 2nd Number : $"
msgEr db 13,10, "Error $"
choiceseu db 13,10, "Enter choice: $ "
sumseu db 13,10,13,10, "***Sum is : $"
Diffseu db 13,10,13,10, "***Difference is : $"
Divseu db 13,10,13,10, "***Quotient is : $"
Mulseu db 13,10,13,10, "***Product is : $"
temp     db ?


.code

start:
mov ax, @data
mov ds, ax

lea dx, msg1
mov ah, 09h
int 21h
mov bx, 0

ph1:
mov ah, 01h
int 21h
cmp al,0dh      
je input1
mov ah,0        
sub al,30h      
push ax         
mov ax,10d      
mul bx          
pop bx          
add bx,ax       
jmp ph1      




input1:
push bx
lea dx,msg2
mov ah,09h
int 21h

mov bx,0


ph2:
mov ah,01h
int 21h
cmp al,0dh
je choice
mov ah,0
sub al,30h
push ax
mov ax,10d
mul bx
pop bx
add bx,ax 
jmp ph2


choice:
lea dx, choiceseu
mov ah, 09h
int 21h

mov ah, 01h
int 21h


cmp al,'4'
je divd

cmp al,'1'  
je addz

cmp al,'2'
je subt

cmp al,'3'
je mult

cmp al,'5'
mov ah, 4ch
int 21h

error:
lea dx,msgEr
mov ah,09h
int 21h 
jmp start


divd: 
pop ax
mov dx, 0
div bx
push ax
lea dx,Divseu
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp wrong

addz:     
pop ax
add ax,bx   
push ax
lea dx,sumseu   
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp wrong   

mult: 
pop ax
mul bx      
push ax     
lea dx,Mulseu   
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp wrong


subt: 
pop ax
sub ax,bx 
push ax
lea dx,Diffseu
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d

wrong:
mov dx, 0
div bx
push dx
mov dx,0
inc cx
or ax,ax 
jne wrong 

ans:        
pop dx
add dl,30h
mov ah,02h
int 21h
loop ans

jmp start



end start

以下是完整代码:

  public static class Function1
    {
        [StorageAccount("AzureWebJobsStorage")]
        [FunctionName("Function1")]
        public static void Run([QueueTrigger("queue")]string myQueueItem,[Queue("output")] out string test2, [Queue("output2", Connection = "AzureWebJobsStorage")] out string itemAddedQueue, TraceWriter log)
        {
            log.Info($"C# Queue trigger function processed: {myQueueItem}");

            dynamic message = JObject.Parse(myQueueItem);

            log.Info($"Received message\n{JsonConvert.SerializeObject(message, Formatting.Indented)}");


            var itemAddedQueueMessage = new test
            {
                name = "test"
            };

            itemAddedQueue = JsonConvert.SerializeObject(itemAddedQueueMessage);

            test2 = JsonConvert.SerializeObject(itemAddedQueueMessage);

            log.Info($"Sent message to queue \"itemAddedQueue\"\n{JsonConvert.SerializeObject(itemAddedQueueMessage, Formatting.Indented)}");
        }

        public class test
        {
            public string name { get; set; }
        }

    }

1 个答案:

答案 0 :(得分:0)

mult: 
pop ax
mul bx      
push ax     
lea dx,Mulseu   
mov ah,09h
int 21h 
pop ax
mov cx,0
mov dx,0
mov bx,10d
jmp wrong

由于你想要从乘法中显示完整的32位结果(在DX:AX中),你不能让DX寄存器浪费掉!您需要像AX一样保留它 因为您当前的转换/显示例程(为什么为什么这个名为错误?)只知道16位数字,所以您需要另一个例程,我将在下面介绍。此例程来自another answer that I wrote some time ago。你一定要读它。它详细解释了这些东西是如何工作的,所以我不会在这里重复这个解释。

mult: 
    pop     ax
    mul     bx      
    push    ax
    push    dx    
    lea     dx, Mulseu   
    mov     ah, 09h
    int     21h 
    pop     dx
    pop     ax
    jmp     DisplayNumber32

    ...

DisplayNumber32:
    mov     bx,10          ;CONST
    push    bx             ;Sentinel
.a: mov     cx,ax          ;Temporarily store LowDividend in CX
    mov     ax,dx          ;First divide the HighDividend
    xor     dx,dx          ;Setup for division DX:AX / BX
    div     bx             ; -> AX is HighQuotient, Remainder is re-used
    xchg    ax,cx          ;Temporarily move it to CX restoring LowDividend
    div     bx             ; -> AX is LowQuotient, Remainder DX=[0,9]
    push    dx             ;(1) Save remainder for now
    mov     dx,cx          ;Build true 32-bit quotient in DX:AX
    or      cx,ax          ;Is the true 32-bit quotient zero?
    jnz     .a             ;No, use as next dividend
    pop     dx             ;(1a) First pop (Is digit for sure)
.b: add     dl,"0"         ;Turn into character [0,9] -> ["0","9"]
    mov     ah,02h         ;DOS.DisplayCharacter
    int     21h            ; -> AL
    pop     dx             ;(1b) All remaining pops
    cmp     dx,bx          ;Was it the sentinel?
    jb      .b             ;Not yet

对于所有剩余的操作( addz sub divd ),您还可以使用这个新的 DisplayNumber32 常规。只需确保事先将DX寄存器归零。

subt: 
    pop     ax
    sub     ax, bx 
    push    ax
    lea     dx, Diffseu
    mov     ah, 09h
    int     21h 
    pop     ax
    xor     dx, dx              ;Add this for the 32-bit version!
    jmp     DisplayNumber32
相关问题