Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; SP, ASM, Lab 2, made by Katolikian Tihran, IK-62
- ; input 2 byte integer; then output 2 byte integer
- ; Data segment byte arrays:
- ; "Buffer" - buffer for inputing integer with max size on highest position;
- ; "Msg_error" - string line with message about any error;
- ; "Msg_enterNum" - message which suggest user to input the integer;
- ; "Msg_pressKey" - message which suggest user to press any key to finish the program;
- ; Interrupts (explanation of interrupts used in this program):
- ; "int 21h {ah = 0ah, ds:dx - input buffer adress}" - buffered input;
- ; "int 21h {ah = 02h, dl = symbol to output}" - output symbol from dl;
- ; "int 21h {ah = 9, dx = str ptr}" - output the string from adress from dx;
- ; "int 16h {ah = 10h}" - read expanded keyboard character
- ; "int 21h {ah = 4ch}" - end program.
- ;data segment
- DSEG segment
- Buffer DB 7, '?', 6 dup('?'), '$'
- Msg_error DB 'Error.$'
- Msg_enterNum DB 'Please, enter a number: $'
- Msg_pressKey DB 'Press any key to show number...', 0Dh, 0Ah, '$'
- DSEG ends
- ;code segment
- CSEG segment
- assume ds:DSEG, cs:CSEG
- Start:
- mov ax, DSEG
- mov ds, ax
- lea dx, Msg_enterNum
- mov ah, 9
- int 21h
- call Get_integer
- cmp di,2
- jz er
- cmp byte ptr [Buffer + 1], 0 ;if no input
- je er
- cmp byte ptr [Buffer + 1], 1
- je Only_sign
- jmp Output
- Only_sign:
- cmp byte ptr [Buffer + 2], '+'
- je er
- cmp byte ptr [Buffer + 2], '-'
- je er
- Output:
- push ax
- lea dx, Msg_pressKey
- mov ah, 9
- int 21h
- mov ah, 10h
- int 16h
- pop ax
- call Print_integer
- mov ah, 10h
- int 16h
- mov ah, 4Ch
- int 21h
- er:
- lea dx, Msg_error
- mov ah,09
- int 21h
- mov ah, 10h
- int 16h
- mov ah, 4ch
- int 21h
- ;proc gets integer in the Buffer
- Get_integer proc
- mov ah,0ah
- xor di,di
- mov dx,offset Buffer
- int 21h ;buff input
- mov dl,0ah ;symbol to output
- mov ah,02
- int 21h ;endl
- mov di,0
- lea si, Buffer + 2 ;to the first symbol
- cmp byte ptr [si],"+"
- je p1
- cmp byte ptr [si],"-"
- jne p2
- mov di,1
- p1:
- inc si
- p2:
- xor ax,ax
- mov bx,10
- p3:
- mov cl,[si]
- cmp cl,0dh
- jz endin ;if it's end of the number, then end input
- cmp cl,'0'
- jb erg ;less than 0
- cmp cl,'9'
- ja erg ;more then 9
- sub cl,'0' ;symbol to number variable
- mul bx
- jc erg ;if we have overflow
- add ax,cx
- ;jc erg
- cmp ax, 8000h
- ja erg
- inc si
- jmp p3
- erg:
- mov di,2
- ret
- endin:
- cmp ax,0
- je newEnd
- cmp di,1
- je p4
- and ax,7fffh ;for 32768
- cmp ax,0
- je erg
- newEnd:
- ret
- p4:
- neg ax
- ret
- Get_integer endp
- Print_integer proc
- mov bx, ax
- or bx, bx ;!
- jns no_minus
- mov al, '-'
- int 29h
- neg bx
- no_minus:
- mov ax,bx
- xor cx,cx
- mov bx,10
- m2:
- xor dx,dx
- div bx
- add dl,'0'
- push dx
- inc cx
- test ax,ax
- jnz m2
- m3:
- pop ax
- int 29h
- loop m3
- ret
- Print_integer endp
- CSEG ends
- END Start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement