# CSE 322 Fall 2018 Lab 7

### Problem #1 (Program listing 9.2, 9.3, 9.4)

Decimal input output procedure

//Solution
.MODEL small
.STACK 256
.DATA

.CODE
MAIN PROC
mov ax,@data
mov ds,ax

call indec
push ax
mov ah, 2
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
pop ax

call outdec

Exit:
mov ah,4Ch
int 21h
MAIN ENDP

outdec proc
push ax
push bx
push cx
push dx

or ax, ax ;Check if ax is 0
jge @end_if1

push ax
mov dl, '-'
mov ah, 2
int 21h
pop ax
neg ax
@end_if1:

xor cx, cx
mov bx, 10
@repeat1:
xor dx, dx
div bx
push dx
inc cx

cmp ax, 0
jne @repeat1

mov ah, 2
@print_loop:
pop dx
int 21h
loop @print_loop

pop dx
pop cx
pop bx
pop ax
ret
outdec endp

indec proc
push bx
push cx
push dx

@begin:
mov ah, 2
mov dl, '?'
int 21h

xor bx, bx
xor cx, cx

mov ah, 1
int 21h

cmp al, '-'
je @minus
cmp al, '+'
je @plus
jmp @repeat2
@minus:
mov cx, 1
@plus:
int 21h
@repeat2:
cmp al, '0'
jnge @not_digit
cmp al, '9'
jnle @not_digit

and ax, 000Fh
push ax

mov ax, 10
mul bx
pop bx

mov ah, 1
int 21h
cmp al, 0dh
jne @repeat2

mov ax, bx
or cx, cx
je @exit
neg ax
@exit:
pop dx
pop cx
pop bx
ret

@not_digit:
mov ah, 2
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
jmp @begin
indec endp
END MAIN

### Problem #2 (Section 8.2)

take 2 decimal numbers greater than 10 as inputs and print their sum and difference afterward

Sample execution
?120
?15
Sum is 135
Difference is 105

//Solution
to be done by students

### Problem #3

Write a procedure to find the factorial of a number stored in cx.

//Solution
.model small
.stack 256
.data
.code

main proc
mov ax, @data
mov ds, ax

mov cx, 4
call fact
call outdec
exit:
mov ah, 4ch
int 21h

main endp

fact proc
mov ax, 1
loop1:
mul cx
loop loop1
ret
fact endp

outdec proc
push ax
push bx
push cx
push dx

or ax, ax
jge @end_if1

push ax
mov dl, '-'
mov ah, 2
int 21h
pop ax
neg ax
@end_if1:

xor cx, cx
mov bx, 10
@repeat1:
xor dx, dx
div bx
push dx
inc cx

cmp ax, 0
jne @repeat1

mov ah, 2
@print_loop:
pop dx
int 21h
loop @print_loop

pop dx
pop cx
pop bx
pop ax
ret
outdec endp
end main