CPE 301 Assignment
CPE 301 Assignment
.model small
.stack 256
CR equ 13d
LF equ 10d
.data
num1 dw ?
num2 dw ?
.code
start:
mov ds, ax
mov num1, ax
mov num2, ax
mov ax, offset result
getn:
; C variables
push bx
push cx
push dx
; Initialize variables
mov dx, -1
newline:
read_loop:
mov ax, 10 ; ax = 10
mul cx ; ax = cx * 10
fin_read:
fin_getn:
pop dx ; Restore registers
pop cx
pop bx
puts:
push ax ; Save ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
pop ax ; Restore ax
putn:
; Display number in ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
mov dx, 0 ; Set dx to 0
cmp ax, 0
; Number is negative
push ax ; Save ax
pop ax ; Restore ax
calc_digits:
; All digits are now on the stack, display them in reverse order
disp_loop:
end_disp_loop:
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
; Display character in al
push ax ; Save ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
pop ax ; Restore ax
getc:
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
end start
Question 2
;; Read and divide two numbers. Display the quotient and remainder.
.model small
.stack 256
CR equ 13d
LF equ 10d
.data
num1 dw ?
num2 dw ?
.code
start:
mov ds, ax
mov num1, ax
getn:
; C variables
push bx
push cx
push dx
; Initialize variables
call getc
mov dx, -1
newline:
read_loop:
mov ax, 10 ; ax = 10
mul cx ; ax = cx * 10
fin_read:
fin_getn:
pop cx
pop bx
puts:
push ax ; Save ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
pop ax ; Restore ax
putn:
; Display number in ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
cmp ax, 0
; Number is negative
push ax ; Save ax
pop ax ; Restore ax
calc_digits:
; All digits are now on the stack, display them in reverse order
disp_loop:
end_disp_loop:
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
putc:
; Display character in al
push ax ; Save ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
pop ax ; Restore ax
getc:
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
mov ah, 1h ; DOS function to read character
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
end start
Question 3:
.model small
.stack 256
CR equ 13d
LF equ 10d
.data
num1 dw ?
num2 dw ?
num3 dw ?
num4 dw ?
.code
start:
mov ds, ax
mov ax, offset prompt1
mov num1, ax
mov num2, ax
mov num3, ax
mov num4, ax
getn:
; C variables
push bx
push cx
push dx
; Initialize variables
call getc
mov dx, -1
newline:
read_loop:
mov ax, 10 ; ax = 10
mul cx ; ax = cx * 10
fin_read:
fin_getn:
pop cx
pop bx
push ax ; Save ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
pop ax ; Restore ax
putn:
; Display number in ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
; Number is negative
push ax ; Save ax
pop ax ; Restore ax
calc_digits:
; All digits are now on the stack, display them in reverse order
disp_loop:
end_disp_loop:
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
putc:
; Display character in al
push ax ; Save ax
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
pop ax ; Restore ax
getc:
push bx ; Save bx
push cx ; Save cx
push dx ; Save dx
pop dx ; Restore dx
pop cx ; Restore cx
pop bx ; Restore bx
end start