Wednesday, 9 December 2015

CS 401 Computer Architecture and Assembly Language Programming Assignment No.2

CS 401 Assignment 2 Code Solution
[ORG 0100H]
jmp start
; subroutine to find factorial of 8
fact:
[ORG 0100H]
MOV AX, 8
MOV CX, AX
XOR DX, DX
DEC CX
CONT: MOV BX, CX
MUL BX
DEC CX
JNZ CONT
MOV DX, AX
ret
; subroutine to clear the screen
clrscr: push es
push ax
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov di, 0 ; point di to top left column
nextloc: mov word [es:di], 0x0720 ; clear next char on screen
add di, 2 ; move to next screen location
cmp di, 4000 ; has the whole screen cleared
jne nextloc ; if no clear next position
pop di
pop ax
pop es
ret
printnum: push bp
mov bp, sp
push es
push ax
push bx
push cx
push dx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov ax, [bp+4] ; load number in ax
mov bx, 10 ; use base 10 for division
mov cx, 0 ; initialize count of digits
nextdigit: mov dx, 0 ; zero upper half of dividend
div bx ; divide by 10
add dl, 0x30 ; convert digit into ascii value
push dx ; save ascii value on stack
inc cx ; increment count of values
cmp ax, 0 ; is the quotient zero
jnz nextdigit ; if no divide it again
mov di, 0 ; point di to top left column
nextpos: pop dx ; remove a digit from the stack
mov dh, 0x07 ; use normal attribute
mov [es:di], dx ; print char on screen
add di, 2 ; move to next screen location
loop nextpos ; repeat for all digits on stack
pop di
pop dx
pop cx
pop bx
pop ax
pop es
pop bp
ret 2
start: call clrscr ; call the clrscr subroutine
call fact
push ax ; place number on stack
call printnum ; call the printnum subroutine
mov ax, 0x4c00 ; terminate programint 
0x21

No comments:

Post a Comment