# CSE 322 Lab 8

April 8, 2018

#### Problem#1 (Chapter 9 program examples)

Multiplication and division instructions usage

//Solution

```mov ax,3
mov bx, 4
mov cx, 2
mul bx
mul cx

mov ax,14
mov bx,3
div bx```

#### Problem#2 (Chapter 9 program examples)

Write a procedure to find the factorial of number N to be stored in CX and the output is provided in AX

//Solution

```title factorial_code
.model small
.stack 100h
.data
.code
main proc

mov cx, 7
call factorial
mov bx, ax
mov ah,2
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
;mov dl, bl
;int 21h
mov ax, bx
call outdec

mov ah, 4ch
int 21h

main 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, 10D

repeat1:
xor dx, dx
div bx
push dx
inc cx

or ax, ax
jne repeat1

mov ah,2

print_loop:
pop dx
or dl, 30h
int 21h
loop print_loop

pop dx
pop cx
pop bx
pop ax
ret
outdec endp

factorial proc
mov ax, 1

top:
mul cx
loop top
ret
factorial endp

end main
```

#### Problem#3 (Chapter 11 example)

//Solution

```title test code
.model small
.stack 100h
.data
string db 80 dup(0)
newline db 0dh,0ah,'\$'
.code
main proc

mov ax, @data
mov ds, ax
mov ES, ax

lea di, string

lea dx, newline
mov ah, 9
int 21h

lea si, string
;mov bx, 10
call print_str

lea dx, newline
mov ah, 9
int 21h

lea si, string
;mov bx, 10
call print_str_reverse

mov ah, 4ch
int 21h

push ax
push di
cld
xor bx, bx
mov ah, 1
int 21h
while1:
cmp al, 0dh
je end_while1

cmp al, 8h
jne else1

dec di
dec bx

else1:
stosb
inc bx
int 21h
jmp while1

end_while1:
pop di
pop ax
ret

print_str proc near
push ax
push bx
push cx
push dx
push si

mov cx, bx
jcxz p_exit
cld

mov ah, 2

top:
lodsb
mov dl, al
int 21h
loop top
p_exit:
pop si
pop dx
pop cx
pop bx
pop ax
ret
print_str endp

print_str_reverse proc near
push ax
push bx
push cx
push dx
push si

dec si

mov cx, bx
jcxz p_exit2
;cld
std
mov ah, 2

top2:
lodsb
mov dl, al
int 21h
loop top2
p_exit2:
pop si
pop dx
pop cx
pop bx
pop ax
ret
print_str_reverse endp

end main```

Problem#3 (Chapter 10 Example 10.4)

Array usage

//Solution

```reverse proc
push ax
push bx
push cx
push dx
push si
push di

mov di, si
mov cx, bx
dec bx
shl bx,1
shr cx,1

xchg_loop:
mov ax,[si]
xchg ax, [di]
mov [si], ax
sub di,2
loop xchg_loop

pop di
pop si
pop dx
pop cx
pop bx
pop ax

ret
reverse endp```

Problem#4 (Chapter 10 Section 10.3)

Selectsort

//Solution

`To be done by students`