Advertisement
Il_Voza

4.1 Switch arithmetic operations

May 12th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Write a program that, given two operands opa and opb of type word in memory, of value respectively of 2043 and 5, executes an operation between integers, chosen by user and that saves the result into the word variable 'res'
  2. ;Depending on the character typed by user, the program must execute:
  3. ;1 --> res = a+b
  4. ;2 --> res = a-b
  5. ;3 --> res = a*b
  6. ;4 --> res = a/b (integer division)
  7.  
  8. ;IMPLEMENTATION
  9. ;Need to implement a switch construct:
  10.  
  11. ;      switch (expression)
  12. ;      {
  13. ;          case val1: sequence1;
  14. ;          break;
  15. ;          case val2: sequence2;
  16. ;          break;
  17. ;          ...
  18. ;          default: sequence_def;
  19. ;      }
  20.  
  21.  
  22. ;It's possible use operations of compare and conditional jumps to a blocks of code
  23.  
  24. .MODEL small
  25. .STACK
  26. .DATA
  27. opa DW 2043
  28. opb DW 5
  29. res DW ?
  30. .CODE
  31. .START
  32. MOV AH,1
  33. INT 21h
  34. SUB AL,'0'
  35. CMP AL,1
  36. JE sum
  37. CMP AL,2
  38. JE subtr
  39. CMP AL,3
  40. JE product
  41. CMP AL,4
  42. JE division
  43. JNE ex
  44.  
  45. sum: MOV BX,opa
  46.        ADD BX,opb
  47.        MOV res, BX
  48.        JMP ex
  49.        
  50. subtr: MOV BX,opa
  51.        SUB BX,opb
  52.        MOV res, BX
  53.        JMP ex
  54.      
  55. product: MOV AX,opa
  56.          MOV CX,opb
  57.          MUL CX
  58.          MOV res,AX
  59.          JMP ex
  60.          
  61. division: MOV AX,opa
  62.           MOV DX,0  ;If I don't insert this, it gives me problem at overflow. Because the value of DX is changed when the input window appears
  63.           MOV BX,opb
  64.           DIV BX
  65.           MOV res,AX
  66.           JMP ex  
  67.          
  68. ex:
  69. .EXIT
  70. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement