Feedjit

Articles for you

Tuesday, December 31, 2013

Switch Case Implementation, Easiest Implementation, Choice Assembly Language, ASM, MASM, NASM Switch CASE

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
;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