0 Replies - 1683 Views - Last Post: 02 October 2012 - 09:57 AM

#1 C++newbieBC22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-September 10

Using emu8086 to write program that performs add, sub, and mult

Posted 02 October 2012 - 09:57 AM

I'm using the emu8086 program to write an assembly language code that performs addition, subtraction, or multiplication after values are entered. The problem that I'm having is that when I perform these operations using negative numbers, it displays negative answers as unsigned integers rather than decimal form (i.e. -100). Maybe I'm missing something, but I can't seem to find out why my program is doing that. Can someone help me please?

.data
SUM db 8 dup(0)
DIFFERENCE db 8 dup(0) 
PRODUCT dw 8 dup(0)
A db 8 DUP(0)
B db 8 DUP(0)
NUMBER DB 10, ?, 10 DUP(0)
DECIMAL_IN_ASCII db 10,? 10 DUP(?)
MENU db '    MENU$'
VALUES db '1 - Enter values$'
ADDITION db '2 - Add$'
SUBTRACTION db '3 - Subtract$'
MULTIPLY db '4 - Multiply$'
QUIT db 'Q - Quit$'
CHOOSE db 'Enter choice of operation: $' 
XPLUSY db 'X + Y = $'
XMINUSY db 'X - Y = $'
XTIMESY db 'X * Y = $'
QUERY_X db 'X: $'
QUERY_Y db 'Y: $'
QUERY db 'Press any key to continue.$'
CLEAR db '                  $'
NUMB db 1010b


.code 
main proc far
          
L5:     mov ah, 06h 
        mov al, 0
        mov bh, 30d
        mov dh, 24
        mov dl, 79
        mov cx, 0  
        int 10h     
        
        mov ah, 2
        mov bh, 0
        mov dh, 0
        mov dl, 2
        int 10h
        
        mov ah, 09
        mov dx, offset MENU
        add dx, 100h
        int 21h    
        
        mov ah, 2
        mov bh, 0
        mov dh, 6
        mov dl, 0
        int 10h 
        
        mov ah, 09
        mov dx, offset VALUES
        add dx, 100h
        int 21h
        
        mov ah, 2
        mov bh, 0
        mov dh, 7
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset ADDITION
        add dx, 100h
        int 21h    
        
        mov ah, 2
        mov bh, 0
        mov dh, 8
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset SUBTRACTION
        add dx, 100h
        int 21h  
        
        mov ah, 2
        mov bh, 0
        mov dh, 9
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset MULTIPLY
        add dx, 100h
        int 21h
        
        mov ah, 2
        mov bh, 0
        mov dh, 11d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset QUIT
        add dx, 100h
        int 21h
        
        mov ah, 2
        mov bh, 0
        mov dh, 13d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset CHOOSE
        add dx, 100h
        int 21h  
        
        mov ah, 2
        mov bh, 0
        mov dh, 16d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset QUERY_X 
        add dx, 100h
        int 21h 
        
        mov ah, 2
        mov bh, 0
        mov dh, 17d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset QUERY_Y
        add dx, 100h
        int 21h
        
        mov ax, 0
        int 33h
        cmp ax, 0
        JE QUITTER
        
        mov ax, 1
        int 33h
        
          
        
        
    START: 
        mov cx, 8  
        mov bx, offset PRODUCT
        add bx, 100h 
        clear1: 
        mov [bx], 0 
        inc bx
        loop clear1
        
        mov cx, 8
        mov bx, offset DIFFERENCE
        add bx, 100h
        clear2: 
        mov [bx], 0 
        inc bx
        loop clear2 
        
        mov cx, 8
        mov bx, offset SUM
        add bx, 100h
        clear3:
        mov [bx], 0
        inc bx
        loop clear3
        
        mov ah, 2
        mov bh, 0
        mov dh, 13d
        mov dl, 28d
        int 10h 
        mov ah, 0
               
      INPUT:       

        mov ah, 1
        int 16h
        
        jnz this
        jz there
        
        this:  
        mov ah, 0
        int 16h
        
        cmp ah, 2
        je compare 
        cmp ah, 3
        je compare 
        cmp ah, 4
        je compare            
        cmp ah, 5 
        je compare
        cmp ah, 10h
           
        there:
        mov ax, 3
        int 33h
        cmp bx, 00000001b
        je release 
        jne INPUT
        release:
            mov ax, 3
            int 33h
            cmp bx, 00000000b    
            je select
            jne release
        select:
            mov ax, dx
            mov bl, 8d
            div bl
            mov dl, al
        correct:
            cmp dl, 6
            je one 
            cmp dl, 7
            je two 
            cmp dl, 8
            je three            
            cmp dl,9 
            je four 
            mov al, 'q'
            jmp compare
                        
            one:     
            mov al, '1' 
            jmp compare
            two:
            mov al, '2' 
            jmp compare
            three:
            mov al, '3'  
            jmp compare
            four:
            mov al, '4'  
            jmp compare
                
            
            
            
            
        
      compare:  
        CMP al, '1'
        JE ENTERVALUE
        CMP al, '2'
        JE ADDER
        CMP al, '3'
        JE SUBBER
        CMP al, '4'
        JE MULT 
        cmp al, 'Q'
        jz QUITTER
        cmp al, 'q'
        QUITTER:
        mov ah, 4ch
        int 21h
          
    ENTERVALUE:
        mov ah, 2
        mov bh, 0
        mov dh, 16d
        mov dl, 3
        int 10h 
        
        mov ah, 09
        mov dx, offset CLEAR
        add dx, 100h
        int 21h 
        
        mov ah, 2
        mov bh, 0
        mov dh, 17d
        mov dl, 3
        int 10h 
        
        mov ah, 09
        mov dx, offset CLEAR
        add dx, 100h
        int 21h
        
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset CLEAR
        add dx, 100h
        int 21h
        
        mov cx, 0
        mov bx, 0
        mov si, offset NUMBER
        
        mov ah, 2
        mov bh, 0
        mov dh, 16d
        mov dl, 3
        int 10h 
        

        
        L1: Call Input_Number
            inc bx
            Loop L1  
            
        Call CONV_ASC2BIN
            mov di, offset NUMBER
            mov bx, offset A 
            add bx, 100h
            mov ax, [di+100h]   
            mov [bx], ax

        
        mov cx, 0
        mov bx, 0  
        mov si, offset NUMBER 
         
         
        mov ah, 2
        mov bh, 0
        mov dh, 17d
        mov dl, 3
        int 10h
         
        L3: Call Input_Number
            inc bx
            loop L3
        Call CONV_ASC2BIN  
            mov di, offset NUMBER
            mov bx, offset B 
            add bx, 100h
            mov ax, [di+100h] 
            mov [bx], ax 
        cmp al, al
        je START
                          
        ADDER:     
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 0
        int 10h 
        
        mov ah, 09
        mov dx, offset CLEAR
        add dx, 100h
        int 21h 
        
        mov SI, offset A
        mov BX, offset B
        mov DI, offset SUM 
          
        Call SUM_A_B    
        mov bx, offset SUM
        add bx, 100h
        mov di, offset DECIMAL_IN_ASCII
        add di, 100h  
        
        mov si, offset NUMB
        add si, 100h
        mov dl, [si]
        
        Call BIN2DEC2ASC 
        mov si, offset SUM
        add si, 100h 
        mov cx, bx
        rewind1:  
            mov al, [di]
            mov [si], al
            dec di
            inc si 
            loop rewind1
            mov [si], '$'
        
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset XPLUSY
        add dx, 100h
        int 21h 
        
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 9
        int 10h  
        
        mov ah, 09       
        mov dx, offset SUM
        add dx, 100h
        int 21h
        cmp al, al
        je START         
      
      SUBBER: 
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 0
        int 10h 
        
        mov ah, 09
        mov dx, offset CLEAR
        add dx, 100h
        int 21h 
        
        mov SI, offset A
        mov BX, offset B
        mov DI, offset DIFFERENCE 
        
        Call SUB_A_B 
        mov bx, offset DIFFERENCE
        add bx, 100h
        mov di, offset DECIMAL_IN_ASCII
        add di, 100h 
        
        mov si, offset NUMB
        add si, 100h
        mov dl, [si]
               
        Call BIN2DEC2ASC 
        mov si, offset DIFFERENCE
        add si, 100h
        mov cx, bx
        rewind2:      
            mov al, [di]
            mov [si], al
            dec di
            inc si 
            loop rewind2
            mov [si], '$'
        
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset XMINUSY
        add dx, 100h
        int 21h 
        
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 9
        int 10h  
        
        mov ah, 09
        mov dx, offset DIFFERENCE
        add dx, 100h
        int 21h
        cmp al, al
        je START     
                
                
        MULT: 
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 0
        int 10h 
        
        mov ah, 09
        mov dx, offset CLEAR
        add dx, 100h
        int 21h         
        
        mov si, offset A
        add si, 100h
        mov bx, offset b
        add bx, 100h
        mov di, offset PRODUCT
        add di, 100h
        
        Call PRODUCT_A_B 
        
        mov bx, offset PRODUCT
        add bx, 100h
        mov di, offset DECIMAL_IN_ASCII
        add di, 100h  
        
        mov si, offset NUMB
        add si, 100h
        mov dl, [si]
        
        Call BIN2DEC2ASC 
        mov si, offset PRODUCT
        add si, 100h 
        mov cx, bx
        rewind3:  
            mov al, [di]
            mov [si], al
            dec di
            inc si 
            loop rewind3
            mov [si], '$'
        
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 0
        int 10h
        
        mov ah, 09
        mov dx, offset XTIMESY
        add dx, 100h
        int 21h 
        
        mov ah, 2
        mov bh, 0
        mov dh, 19d
        mov dl, 9
        int 10h  
        
        mov ah, 09       
        mov dx, offset PRODUCT
        add dx, 100h
        int 21h
        cmp al, al
        je START          
               
                
                
                  
        mov ah, 2
        mov bh, 0
        mov dh, 7
        mov dl, 0
        int 10h  
        
        mov ah, 9         
        mov dx, offset QUERY
        add dx, 100h
        int 21h
        
        mov ah, 1
        int 21h
         
        jmp L5
         
    
    main endp

SUM_A_B proc near
    CLC
        mov AX, [SI+100h]
        ADC AX, [BX+100h]
        mov [DI+100h], AX  
        ret
    SUM_A_B endp

SUB_A_B proc near
    CLC
        mov AX, [SI+100h]
        SBB AX, [BX+100h]
        mov [DI+100h], AX
        ret
    SUB_A_B endp    

Input_Number proc near
    mov ah, 1 
    int 21h
    cmp al, 'Q'
    jz QUITT
    cmp al, 'q'
    jnz Keep
    QUITT:
    mov ah, 4ch
    int 21h
    ret
    Keep: mov [si+100h], al
    inc si
    cmp al, 0dh
    jz LAST
    mov cx, 2
    ret
    LAST: mov cx, 1
        ret
    Input_Number endp

CONV_ASC2BIN proc near 
    dec bx
    push bx
    mov cx, bx
    mov bx, 0
    push bx  
    mov SI, offset NUMBER  
    add si, 100h      
    CONV:
        mov ax, 0  
        mov bx, 10d
        mov al,[si]
        sub al, 30h
        push cx
        dec cx
        cmp cx, 0
        jz ONES
        TIMES: mul bx
            loop TIMES 
        inc si 
        pop cx 
        pop bx
        add bx, ax
        push bx
        loop CONV
     ONES:
     pop cx    
     pop bx
     add bx, ax 
     mov si, offset NUMBER
     add si, 100h
        mov [si], bx
        pop bx
        ret
    CONV_ASC2BIN endp 

BIN2DEC2ASC proc near
    mov ax, [bx] 
    mov dx, [bx+2] 
    mov bx, 0
    ANOTHER:
    mov cx, 10d
    DIV cx
    add dx, 30h
    mov [di], dl
    inc di
    inc bx
    mov dx, 0
    cmp ax, 0
    jnz ANOTHER 
            
    mov [di], '$' 
    dec di
    
    ret   

PRODUCT_A_B proc near
    mov ax, [si]
    mov dx, [bx]
    push dx 
    mov [di], 0
    mov cx, 0
    
    STARTER:
        and dx, 1
        cmp dx, 1
        JE INCREASE
        JNE NONE
        
        INCREASE:
            add [di], ax 
            clc
            shl ax, 1 
   back:    JO BIG
            pop dx
            clc
            rcr dx,1
            push dx
            cmp dx, 0
            je FINISH
            jne STARTER  
            
            SHIFT:
                shl cx, 1
                jmp back
            
            BIG:
                cmp cx, 0
                je GROW
                JNE STAY
                GROW:
                inc cx 
                jmp here
                STAY:
                shl cx,1
                here:pop dx
                clc
                rcr dx, 1
                push dx
                cmp dx, 0
                je FINISH
                jne STARTER
            
        NONE:
            clc
            shl ax, 1 
            JO BIG
            pop dx
            clc
            rcr dx,1
            push dx
            cmp dx, 0
            je FINISH
            jne STARTER
            
        FINISH: 
            add di, 2  
            shr cx, 1
            add [di], cx
            pop dx
            ret 
 
            
           
endp                          

end main


Is This A Good Question/Topic? 0
  • +

Page 1 of 1