为这个简单的计算器添加电源功能

问题描述 投票:0回答:1

我有一个简单的计算器,如下所示。我想添加幂功能,以便计算器可以计算 2 个数字的幂,这些值取自用户输入。

这是4个主要操作的代码。我想向该程序添加电源功能。我尝试了不同的方法,但没有成功。下面的代码通过 emu8086 工作,但我想向该程序添加电源功能。有什么想法我该如何处理这个问题?


org 100h

jmp start       ; jump over data declaration

msg:    db      "1-Add",0dh,0ah,"2-Multiply",0dh,0ah,"3-Subtract",0dh,0ah,"4-Divide", 0Dh,0Ah, '$'
msg2:    db      0dh,0ah,"Enter First No: $"
msg3:    db      0dh,0ah,"Enter Second No: $"
msg4:    db      0dh,0ah,"Choice Error $" 
msg5:    db      0dh,0ah,"Result : $" 
msg6:    db      0dh,0ah ,'thank you for using the simple calculator! press any key to exit... ', 0Dh,0Ah, '$'

start:  mov ah,9
        mov dx, offset msg ;first , we initialize the program with showing a simple prompt that ask the user to choose one operation on key press
        int 21h
        mov ah,0                       
        int 16h  ;then we will use int 16h to read a key press, to know the operation he/she choosed
        cmp al,31h ;the keypress will be stored in al so, we will comapre to 1 addition ..........
        je Addition
        cmp al,32h ; if the keypress wasn't equal to 1 or addition ,we check that with 2 multiply
        je Multiply
        cmp al,33h   ; if the keypress wasn't equal to 1 or addition ,we check that with 3 subtract
        je Subtract
        cmp al,34h     ; if the keypress wasn't equal to 1 or addition ,we check that with 3 Devide
        je Divide
        mov ah,09h
        mov dx, offset msg4
        int 21h
        mov ah,0
        int 16h
        jmp start
        
Addition:   mov ah,9  ;then let us handle the case of addition operation
            mov dx, offset msg2  ;first we will display this message enter first no also using int 21h
            int 21h
            mov cx,0 ;we will call InputNo to handle our input as we will take each number seprately
            call InputNo  ;first we will move to cx 0 because we will increment on it later in InputNo
            push dx
            mov ah,9
            mov dx, offset msg3
            int 21h 
            mov cx,0
            call InputNo
            pop bx
            add dx,bx
            push dx 
            mov ah,9
            mov dx, offset msg5
            int 21h
            mov cx,10000
            pop dx
            call View 
            jmp exit 
            
InputNo:    mov ah,0
            int 16h ;then we will use int 16h to read a key press     
            mov dx,0  
            mov bx,1 
            cmp al,0dh ;the keypress will be stored in al so, we will comapre to  0d which represent the enter key, to know wheter he finished entering the number or not 
            je FormNo ;if it's the enter key then this mean we already have our number stored in the stack, so we will return it back using FormNo
            sub ax,30h ;we will subtract 30 from the the value of ax to convert the value of key press from ascii to decimal
            call ViewNo ;then call ViewNo to view the key we pressed on the screen
            mov ah,0 ;we will mov 0 to ah before we push ax to the stack bec we only need the value in al
            push ax  ;push the contents of ax to the stack
            inc cx   ;we will add 1 to cx as this represent the counter for the number of digit
            jmp InputNo ;then we will jump back to input number to either take another number or press enter          
   

;we took each number separatly so we need to form our number and store in one bit for example if our number 235
FormNo:     pop ax; Take the last input from the stack  
            push dx      
            mul bx;Here we are multiplying the value of ax with the value of bx
            pop dx;After multiplication we will remove it from stack
            add dx,ax;After removing from stack add the value of dx with the value of ax
            mov ax,bx;Then set the value of bx in ax       
            mov bx,10
            push dx;push the dx value again in stack before multiplying to resist any kind of accidental effect
            mul bx;Multiply bx value by 10
            pop dx;pop the dx after multiplying
            mov bx,ax;Result of the multiplication is still stored in ax so we need to move it in bx
            dec cx;After moving the value we will decrement the digit counter value
            cmp cx,0;Check if the cx counter is 0
            jne FormNo;If the cx counter is not 0 that means we have multiple digit input and we need to run format number function again
            ret;If the cx counter is 0 that means all of our digits are fully formatted and stored in bx we just need to return the function   


       
       
View:  mov ax,dx
       mov dx,0
       div cx 
       call ViewNo
       mov bx,dx 
       mov dx,0
       mov ax,cx 
       mov cx,10
       div cx
       mov dx,bx 
       mov cx,ax
       cmp ax,0
       jne View
       ret


ViewNo:    push ax ;we will push ax and dx to the stack because we will change there values while viewing then we will pop them back from
           push dx ;the stack we will do these so, we don't affect their contents
           mov dx,ax ;we will mov the value to dx as interrupt 21h expect that the output is stored in it
           add dl,30h ;add 30 to its value to convert it back to ascii
           mov ah,2
           int 21h
           pop dx  
           pop ax
           ret
      
   
exit:   mov dx,offset msg6
        mov ah, 9
        int 21h  


        mov ah, 0
        int 16h

        ret
            
                       
Multiply:   mov ah,9
            mov dx, offset msg2
            int 21h
            mov cx,0
            call InputNo
            push dx
            mov ah,9
            mov dx, offset msg3
            int 21h 
            mov cx,0
            call InputNo
            pop bx
            mov ax,dx
            mul bx 
            mov dx,ax
            push dx 
            mov ah,9
            mov dx, offset msg5
            int 21h
            mov cx,10000
            pop dx
            call View 
            jmp exit 


Subtract:   mov ah,9
            mov dx, offset msg2
            int 21h
            mov cx,0
            call InputNo
            push dx
            mov ah,9
            mov dx, offset msg3
            int 21h 
            mov cx,0
            call InputNo
            pop bx
            sub bx,dx
            mov dx,bx
            push dx 
            mov ah,9
            mov dx, offset msg5
            int 21h
            mov cx,10000
            pop dx
            call View 
            jmp exit 
    
            
Divide:     mov ah,9
            mov dx, offset msg2
            int 21h
            mov cx,0
            call InputNo
            push dx
            mov ah,9
            mov dx, offset msg3
            int 21h 
            mov cx,0
            call InputNo
            pop bx
            mov ax,bx
            mov cx,dx
            mov dx,0
            mov bx,0
            div cx
            mov bx,dx
            mov dx,ax
            push bx 
            push dx 
            mov ah,9
            mov dx, offset msg5
            int 21h
            mov cx,10000
            pop dx
            call View
            pop bx
            cmp bx,0
            je exit 
            jmp exit
assembly masm tasm emu8086
1个回答
0
投票

第一个输入的数字是底数,第二个输入的数字是幂。请记住,零的幂会导致结果 1!不要使用大数字,因为它可能会很快溢出。

5^0 = 1
5^1 = 5
5^2 = 25
5^3 = 125

在顶部省略号处填写您的输入代码,并在底部省略号处填写您的显示代码。


  ...

  call InputNo  ; -> DX
  pop  ax       ; First number is the base
  mov  cx, dx   ; Second number is the power
  mov  dx, 1    ; If power is 0 then by definition result is 1
  sub  cx, 1    ; Produces 'below' if CX was 0
                ; Produces 'zero' if CX was 1
  jb   .done    ; Inputted power was 0, so we return DX=1
  jz   .xfer    ; Inputted power was 1, so we return DX=Num1
.more:
  mul  ax       ; Ignoring DX here...
  dec  cx
  jnz  .more
.xfer:
  mov  dx, ax
.done:

  ...


您的 FormNo 代码可以在顶部添加

jcxz
!如果用户碰巧按了不带任何数字的 Enter 键,则堆栈中不会有
pop

FormNo:     jcxz .None
            pop  ax
            push dx      
            mul  bx
            pop  dx
            add  dx,ax
            mov  ax,bx       
            mov  bx,10
            push dx
            mul  bx
            pop  dx
            mov  bx,ax
            dec  cx
            jnz  FormNo
.None:      ret
© www.soinside.com 2019 - 2024. All rights reserved.