Advertisement
Guest User

Lab 2

a guest
Nov 12th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; SP, ASM, Lab 2, made by Katolikian Tihran, IK-62
  2. ; input 2 byte integer; then output 2 byte integer
  3. ; Data segment byte arrays:
  4.     ; "Buffer" - buffer for inputing integer with max size on highest position;
  5.     ; "Msg_error" - string line with message about any error;
  6.     ; "Msg_enterNum" - message which suggest user to input the integer;
  7.     ; "Msg_pressKey" - message which suggest user to press any key to finish the program;
  8. ; Interrupts (explanation of interrupts used in this program):
  9.     ; "int 21h {ah = 0ah, ds:dx - input buffer adress}" - buffered input;
  10.     ; "int 21h {ah = 02h, dl = symbol to output}" - output symbol from dl;
  11.     ; "int 21h {ah = 9, dx = str ptr}" - output the string from adress from dx;
  12.     ; "int 16h {ah = 10h}" - read expanded keyboard character
  13.     ; "int 21h {ah = 4ch}" - end program.
  14.  
  15.  
  16. ;data segment
  17. DSEG segment
  18.     Buffer DB 7, '?', 6 dup('?'), '$'
  19.     Msg_error DB 'Error.$'
  20.     Msg_enterNum DB 'Please, enter a number: $'
  21.     Msg_pressKey DB 'Press any key to show number...', 0Dh, 0Ah, '$'
  22. DSEG ends
  23.  
  24. ;code segment
  25. CSEG segment
  26.     assume ds:DSEG, cs:CSEG
  27.     Start:
  28.     mov ax, DSEG
  29.     mov ds, ax
  30.    
  31.     lea dx, Msg_enterNum
  32.     mov ah, 9
  33.     int 21h
  34.    
  35.     call Get_integer
  36.     cmp di,2
  37.     jz er
  38.    
  39.     cmp byte ptr [Buffer + 1], 0    ;if no input
  40.     je er
  41.     cmp byte ptr [Buffer + 1], 1
  42.     je Only_sign
  43.    
  44.     jmp Output
  45.    
  46.     Only_sign:
  47.     cmp byte ptr [Buffer + 2], '+'
  48.     je er
  49.     cmp byte ptr [Buffer + 2], '-'
  50.     je er
  51.    
  52.     Output:
  53.    
  54.     push ax
  55.     lea dx, Msg_pressKey
  56.     mov ah, 9
  57.     int 21h
  58.     mov ah, 10h
  59.     int 16h
  60.     pop ax
  61.    
  62.     call Print_integer
  63.     mov ah, 10h
  64.     int 16h
  65.     mov ah, 4Ch
  66.     int 21h
  67.    
  68.     er:
  69.     lea dx, Msg_error
  70.     mov ah,09
  71.     int 21h
  72.     mov ah, 10h
  73.     int 16h
  74.     mov ah, 4ch
  75.     int 21h
  76.  
  77.     ;proc gets integer in the Buffer
  78. Get_integer proc
  79.  
  80.     mov ah,0ah
  81.     xor di,di
  82.     mov dx,offset Buffer
  83.     int 21h     ;buff input
  84.     mov dl,0ah  ;symbol to output
  85.     mov ah,02  
  86.     int 21h     ;endl
  87.  
  88.     mov di,0
  89.     lea si, Buffer + 2  ;to the first symbol
  90.     cmp byte ptr [si],"+"
  91.     je p1
  92.     cmp byte ptr [si],"-"
  93.     jne p2
  94.     mov di,1
  95.     p1:  
  96.     inc si    
  97.     p2:
  98.     xor ax,ax
  99.     mov bx,10  
  100.     p3:
  101.     mov cl,[si]
  102.     cmp cl,0dh  
  103.     jz endin    ;if it's end of the number, then end input
  104.    
  105.  
  106.     cmp cl,'0'  
  107.     jb erg      ;less than 0
  108.     cmp cl,'9'
  109.     ja erg      ;more then 9
  110.  
  111.     sub cl,'0'  ;symbol to number variable
  112.     mul bx
  113.     jc erg      ;if we have overflow
  114.     add ax,cx
  115.     ;jc erg      
  116.     cmp ax, 8000h
  117.     ja erg      
  118.     inc si    
  119.     jmp p3  
  120.  
  121.     erg:  
  122.     mov di,2
  123.     ret
  124.  
  125.     endin:
  126.     cmp ax,0
  127.     je newEnd
  128.     cmp di,1
  129.     je p4
  130.     and ax,7fffh    ;for 32768
  131.     cmp ax,0
  132.     je erg
  133.     newEnd:
  134.     ret
  135.    
  136.     p4:
  137.     neg ax
  138.     ret
  139. Get_integer endp
  140.  
  141. Print_integer proc
  142.  
  143.     mov bx, ax
  144.     or bx, bx  ;!
  145.     jns no_minus
  146.     mov al, '-'
  147.     int 29h
  148.     neg bx
  149.     no_minus:
  150.     mov ax,bx
  151.     xor cx,cx
  152.     mov bx,10
  153.     m2:
  154.     xor dx,dx
  155.     div bx
  156.     add dl,'0'
  157.     push dx
  158.     inc cx
  159.     test ax,ax
  160.     jnz m2
  161.     m3:
  162.     pop ax
  163.     int 29h
  164.     loop m3
  165.     ret
  166.  
  167. Print_integer endp
  168.  
  169. CSEG ends
  170. END Start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement