Feedjit

Articles for you

Showing posts with label Add with Carry. Show all posts
Showing posts with label Add with Carry. Show all posts

Saturday, June 1, 2013

Simplest Implementaion of Switch Case in Assembly Language .asm file MASM file 32-bit/64bit

;Programmed by SAQIB KHAN
;Purpose: Obtain Choice from user/Implement case
;Dated: 15th-MAY-2012

.model small
.stack 100h
.data
        num1 db 97
        num2 db 2
    msg_add db 10,  "Press 0 for  Addition $"
        msg_sub db 10,  "Press 1 for  Subtraction $"
    msg_adc db 10,  "Press 2 to add with carry $"
    message db 10,  "Enter your choice: $"
msg_wrong db 10,  "Please enter correct choice $"
msg_verify db 10, "First operand sholud be greater than 2nd,Please change input values $"
.code
main: 
        mov ax,@data
        mov ds,ax
                       
    lea dx,msg_add    ;Display add message
    mov ah,9h
    int 21h
   
    lea dx,msg_sub    ;Display subtract message
    mov ah,9h
    int 21h

    lea dx,msg_adc    ;Display add message
    mov ah,9h
    int 21h
   
    lea dx,message  ;Display choice message
    mov ah,9h
    int 21h
;;;;;;;;;;;;;;;;;;;;   
   
    mov ah,1                 ;read key from user
    int 21h
    mov ah,0                 ;empty the ah ,upper part of ax
   
    sub al,30h              ; subtract 30h to get exact value from ascii
   
    shl al,1                ; multiply number by 2 to get to the case
    cmp al,4                ;compare for validity
    jg check                ;jump if al,greater than 4
    mov bx,ax               ;mov value of ax to bx for offset
    jmp cs:jmptble [bx]          ;jumptable

jmptble dw case0,case1,case2 ;cases   
   

check:
    lea dx,msg_wrong        ;Display wrong choice
    mov ah,9h           
    int 21h
    mov dl,10            ;Move to new line
    mov ah,2h
    int 21h
    jmp main            ;Loop again to get correct choice
case0:                    ;Case0- Addition
    mov dl,10            ;move to new line
    mov ah,2h
    int 21h
   
    mov al,num1            ;copy num1 to al
    xor ah,ah            ;empty ah to zero   
    add al,num2             ; add num2 to al
    call display   
    jmp endlabel            ;jum to endlabel
case1:                    ;Case1 for subtraction
    mov dl,10            ;;Move to new line
    mov ah,2h
    int 21h

    mov cl,num1            ;Copy number1 to cl
    cmp cl,num2            ;check if first numbr is smaller than second
    jl  verify            ;jump if num1 is less than num2
   
    mov al,num1            ;copy num1 to al
        xor ah,ah            ;empty the uper portion of ax
        sub al,num2             ;subtract two nmbers
   
    call display   
    jmp endlabel
case2:
    mov dl,10
    mov ah,2h
    int 21h
   
        mov al,num1
        xor ah,ah   
        adc al,num2     ;adc add 1 value to the sum (if carry flag is 1)
   
        call display
        jmp endlabel

verify:   
        lea dx,msg_verify
    mov ah,9
    int 21h
display:
mov dl,10            ;mov dl,10 to divide result in ax
        div dl                ;Dvide ax by 10
        mov dl,al            ;copy quotient to dl
        mov bl,ah            ;copy remainder to bl
        add dl,30h                ;add 30h to get actual number
        mov ah,2h            ;Service # to display character
    int 21h                ;Call OS to do the job
    add bl,30h            ;add 30h to remainder
    mov dl,bl            ;move to dl to display
        mov ah,2h            ;Service # to display character
        int 21h             ;Call OS to do the job
        ret
   

endlabel:
    mov ah,4ch
    int 21h

end main

Read More

Articles for you