0% found this document useful (0 votes)
47 views

CPE 301 Assignment

The program reads two numbers from the user, divides the first number by the second, and displays the quotient and remainder. It prompts the user to enter the first number, reads the input, prompts for the second number, reads it, divides the first number by the second, displays the quotient, and then displays the remainder. All input and output is done through DOS interrupts and the numbers are stored and processed using registers.

Uploaded by

Faisal Yussuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

CPE 301 Assignment

The program reads two numbers from the user, divides the first number by the second, and displays the quotient and remainder. It prompts the user to enter the first number, reads the input, prompts for the second number, reads it, divides the first number by the second, displays the quotient, and then displays the remainder. All input and output is done through DOS interrupts and the numbers are stored and processed using registers.

Uploaded by

Faisal Yussuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Question 1

.model small

.stack 256

CR equ 13d

LF equ 10d

.data

prompt1 db 'Enter first number: $'

prompt2 db CR, LF, 'Enter second number: $'

result db CR, LF, 'The product is $'

num1 dw ?

num2 dw ?

.code

start:

mov ax, @data

mov ds, ax

mov ax, offset prompt1

call puts ; display prompt1

call getn ; read first number

mov num1, ax

mov ax, offset prompt2

call puts ; display prompt2

call getn ; read second number

mov num2, ax
mov ax, offset result

call puts ; display result message

mov ax, num1

imul num2 ; Multiply num1 and num2

call putn ; display product

mov ax, 4c00h

int 21h ; finished, back to DOS

getn:

; Read a number from the keyboard

; return value in AX register

; C variables

;dx records sign of number sign variable

;bl stores each digit digit variable

;cx stores the number read in so far n variable

;al stores each character rad in .

;ax is also used in the mul instruction

; Save registers on the stack

push bx

push cx

push dx

; Initialize variables

mov dx, 1 ; Record sign, 1 for positive

mov bx, 0 ; Initialize digit to 0

mov cx, 0 ; Initialize number to 0

; Read first character


call getc

cmp al, '-' ;it is negative

jne newline ; If not negative, goto newline

; Record sign for negative number

mov dx, -1

call getc ; Get next digit

newline:

push dx ; Save sign on stack

cmp al, 13 ; Check if al == CR

je fin_read ; If yes, goto fin_read

sub al, '0' ; Convert to digit

mov cl, al ; cl = first digit

call getc ; Get next character

read_loop:

cmp al, 13 ; If (al == CR)

je fin_read ; Then goto fin_read

sub al, '0' ; Otherwise, convert to digit

mov bl, al ; bl = digit

mov ax, 10 ; ax = 10

mul cx ; ax = cx * 10

mov cx, ax ; cx = ax, n = n * 10

add cx, bx ; cx = cx + digit, n = n + digit

call getc ; Read next digit

jmp read_loop ; Repeat the loop

fin_read:

mov ax, cx ; Number returned in ax

pop dx ; Retrieve sign from stack

cmp dx, 1 ; If dx == 1 (positive)

je fin_getn ; Then jump to fin_getn

neg ax ; Otherwise, negate ax (ax = -ax)

fin_getn:
pop dx ; Restore registers

pop cx

pop bx

ret ; Return from the subroutine

puts:

; Display a string terminated by $

; DX contains the address of the string

push ax ; Save ax

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dx, ax ; Set dx to the address of the string

mov ah, 9h ; DOS function to display string

int 21h ; Call MS-DOS to output string

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

pop ax ; Restore ax

ret ; Return from the subroutine

putn:

; Display number in ax

; ax contains number (and also div C in above)

; dx contains remainder (rem in C above)

; cx contains 10 for division

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx
mov dx, 0 ; Set dx to 0

push dx ; Push 0 as a sentinel

mov cx, 10 ; Set cx to 10

cmp ax, 0

jge calc_digits ; If number is non-negative, jump to calc_digits

; Number is negative

neg ax ; Make ax positive

push ax ; Save ax

mov al, '-' ; Display '-' sign

call putc ; Call putc to display the sign

pop ax ; Restore ax

calc_digits:

div cx ; dx:ax = ax / cx, ax = result, dx = remainder

add dx, '0' ; Convert dx to ASCII digit

push dx ; Save digit on the stack

mov dx, 0 ; Reset dx to 0

cmp ax, 0 ; Check if finished

jne calc_digits ; If not, repeat the process

; All digits are now on the stack, display them in reverse order

disp_loop:

pop dx ; Get the last digit from the stack

cmp dx, 0 ; Check if it's the sentinel

je end_disp_loop ; If yes, end the display

mov al, dl ; Move the digit to al

call putc ; Display the digit

jmp disp_loop ; Repeat the process for the next digit

end_disp_loop:

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

ret ; Return from the subroutine


putc:

; Display character in al

push ax ; Save ax

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dl, al ; Move character to dl

mov ah, 2h ; DOS function to display character

int 21h ; Call MS-DOS to display character

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

pop ax ; Restore ax

ret ; Return from the subroutine

getc:

; Read character into al

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov ah, 1h ; DOS function to read character

int 21h ; Call MS-DOS to read character

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

ret ; Return from the subroutine

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

prompt1 db 'Enter first number: $'

prompt2 db CR, LF,'Enter second number: $'

quotient db CR, LF, 'The quotient is $'

remainder db CR, LF, 'The remainder is $'

num1 dw ?

num2 dw ?

.code

start:

mov ax, @data

mov ds, ax

; Prompt for the first number

mov ax, offset prompt1

call puts ; display prompt1

call getn ; read first number

mov num1, ax

; Prompt for the second number

mov ax, offset prompt2

call puts ; display prompt2

call getn ; read second number


mov num2, ax ; Store the second number in a ax register

; Display quotient message

mov ax, offset quotient

call puts ; display quotient message

; Divide num1 by the second number (bx)

mov ax, num1

xor dx, 0 ; Clear dx for division

idiv num2 ; ax=ax/num2,dx stores the remainder

call putn ; display quotient

; Display remainder message

mov ax, offset remainder

call puts ; display remainder message

mov ax, dx ; Copy remainder from dx to ax

call putn ; display remainder

mov ax, 4c00h

int 21h ; finished, back to DOS

getn:

; Read a number from the keyboard

; return value in AX register

; C variables

;dx records sign of number sign variable

;bl stores each digit digit variable

;cx stores the number read in so far n variable

;al stores each character rad in .

;ax is also used in the mul instruction

; Save registers on the stack

push bx
push cx

push dx

; Initialize variables

mov dx, 1 ; Record sign, 1 for positive

mov bx, 0 ; Initialize digit to 0

mov cx, 0 ; Initialize number to 0

; Read first character

call getc

cmp al, '-' ;it is negative

jne newline ; If not negative, goto newline

; Record sign for negative number

mov dx, -1

call getc ; Get next digit

newline:

push dx ; Save sign on stack

cmp al, 13 ; Check if al == CR

je fin_read ; If yes, goto fin_read

sub al, '0' ; Convert to digit

mov cl, al ; cl = first digit

call getc ; Get next character

read_loop:

cmp al, 13 ; If (al == CR)

je fin_read ; Then goto fin_read

sub al, '0' ; Otherwise, convert to digit

mov bl, al ; bl = digit

mov ax, 10 ; ax = 10

mul cx ; ax = cx * 10

mov cx, ax ; cx = ax, n = n * 10

add cx, bx ; cx = cx + digit, n = n + digit


call getc ; Read next digit

jmp read_loop ; Repeat the loop

fin_read:

mov ax, cx ; Number returned in ax

pop dx ; Retrieve sign from stack

cmp dx, 1 ; If dx == 1 (positive)

je fin_getn ; Then jump to fin_getn

neg ax ; Otherwise, negate ax (ax = -ax)

fin_getn:

pop dx ; Restore registers

pop cx

pop bx

ret ; Return from the subroutine

puts:

; Display a string terminated by $

; DX contains the address of the string

push ax ; Save ax

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dx, ax ; Set dx to the address of the string

mov ah, 9h ; DOS function to display string

int 21h ; Call MS-DOS to output string

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

pop ax ; Restore ax

ret ; Return from the subroutine

putn:
; Display number in ax

; ax contains number (and also div C in above)

; dx contains remainder (rem in C above)

; cx contains 10 for division

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dx, 0 ; Set dx to 0

push dx ; Push 0 as a sentinel

mov cx, 10 ; Set cx to 10

cmp ax, 0

jge calc_digits ; If number is non-negative, jump to calc_digits

; Number is negative

neg ax ; Make ax positive

push ax ; Save ax

mov al, '-' ; Display '-' sign

call putc ; Call putc to display the sign

pop ax ; Restore ax

calc_digits:

div cx ; dx:ax = ax / cx, ax = result, dx = remainder

add dx, '0' ; Convert dx to ASCII digit

push dx ; Save digit on the stack

mov dx, 0 ; Reset dx to 0

cmp ax, 0 ; Check if finished

jne calc_digits ; If not, repeat the process

; All digits are now on the stack, display them in reverse order

disp_loop:

pop dx ; Get the last digit from the stack

cmp dx, 0 ; Check if it's the sentinel


je end_disp_loop ; If yes, end the display

mov al, dl ; Move the digit to al

call putc ; Display the digit

jmp disp_loop ; Repeat the process for the next digit

end_disp_loop:

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

ret ; Return from the subroutine

putc:

; Display character in al

push ax ; Save ax

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dl, al ; Move character to dl

mov ah, 2h ; DOS function to display character

int 21h ; Call MS-DOS to display character

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

pop ax ; Restore ax

ret ; Return from the subroutine

getc:

; Read character into al

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx
mov ah, 1h ; DOS function to read character

int 21h ; Call MS-DOS to read character

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

ret ; Return from the subroutine

end start

Question 3:

.model small

.stack 256

CR equ 13d

LF equ 10d

.data

prompt1 db 'Enter first number: $'

prompt2 db CR, LF, 'Enter second number: $'

prompt3 db CR, LF, 'Enter third number: $'

prompt4 db CR, LF, 'Enter fourth number: $'

result db CR, LF, 'The sum is $'

num1 dw ?

num2 dw ?

num3 dw ?

num4 dw ?

.code

start:

mov ax, @data

mov ds, ax
mov ax, offset prompt1

call puts ; display prompt1

call getn ; read first number

mov num1, ax

mov ax, offset prompt2

call puts ; display prompt2

call getn ; read second number

mov num2, ax

mov ax, offset prompt3

call puts ; display prompt3

call getn ; read third number

mov num3, ax

mov ax, offset prompt4

call puts ; display prompt4

call getn ; read fourth number

mov num4, ax

; Display result message

mov ax, offset result

call puts ; display result message

; Calculate and display the sum of four numbers

mov ax, num1

add ax, num2

add ax, num3


add ax, num4

call putn ; display sum

mov ax, 4c00h

int 21h ; finished, back to DOS

getn:

; Read a number from the keyboard

; return value in AX register

; C variables

;dx records sign of number sign variable

;bl stores each digit digit variable

;cx stores the number read in so far n variable

;al stores each character rad in .

;ax is also used in the mul instruction

; Save registers on the stack

push bx

push cx

push dx

; Initialize variables

mov dx, 1 ; Record sign, 1 for positive

mov bx, 0 ; Initialize digit to 0

mov cx, 0 ; Initialize number to 0

; Read first character

call getc

cmp al, '-' ;it is negative

jne newline ; If not negative, goto newline


; Record sign for negative number

mov dx, -1

call getc ; Get next digit

newline:

push dx ; Save sign on stack

cmp al, 13 ; Check if al == CR

je fin_read ; If yes, goto fin_read

sub al, '0' ; Convert to digit

mov cl, al ; cl = first digit

call getc ; Get next character

read_loop:

cmp al, 13 ; If (al == CR)

je fin_read ; Then goto fin_read

sub al, '0' ; Otherwise, convert to digit

mov bl, al ; bl = digit

mov ax, 10 ; ax = 10

mul cx ; ax = cx * 10

mov cx, ax ; cx = ax, n = n * 10

add cx, bx ; cx = cx + digit, n = n + digit

call getc ; Read next digit

jmp read_loop ; Repeat the loop

fin_read:

mov ax, cx ; Number returned in ax

pop dx ; Retrieve sign from stack

cmp dx, 1 ; If dx == 1 (positive)

je fin_getn ; Then jump to fin_getn

neg ax ; Otherwise, negate ax (ax = -ax)

fin_getn:

pop dx ; Restore registers

pop cx

pop bx

ret ; Return from the subroutine


puts:

; Display a string terminated by $

; DX contains the address of the string

push ax ; Save ax

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dx, ax ; Set dx to the address of the string

mov ah, 9h ; DOS function to display string

int 21h ; Call MS-DOS to output string

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

pop ax ; Restore ax

ret ; Return from the subroutine

putn:

; Display number in ax

; ax contains number (and also div C in above)

; dx contains remainder (rem in C above)

; cx contains 10 for division

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dx, 0 ; Set dx to 0

push dx ; Push 0 as a sentinel

mov cx, 10 ; Set cx to 10


cmp ax, 0

jge calc_digits ; If number is non-negative, jump to calc_digits

; Number is negative

neg ax ; Make ax positive

push ax ; Save ax

mov al, '-' ; Display '-' sign

call putc ; Call putc to display the sign

pop ax ; Restore ax

calc_digits:

div cx ; dx:ax = ax / cx, ax = result, dx = remainder

add dx, '0' ; Convert dx to ASCII digit

push dx ; Save digit on the stack

mov dx, 0 ; Reset dx to 0

cmp ax, 0 ; Check if finished

jne calc_digits ; If not, repeat the process

; All digits are now on the stack, display them in reverse order

disp_loop:

pop dx ; Get the last digit from the stack

cmp dx, 0 ; Check if it's the sentinel

je end_disp_loop ; If yes, end the display

mov al, dl ; Move the digit to al

call putc ; Display the digit

jmp disp_loop ; Repeat the process for the next digit

end_disp_loop:

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

ret ; Return from the subroutine

putc:

; Display character in al

push ax ; Save ax
push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov dl, al ; Move character to dl

mov ah, 2h ; DOS function to display character

int 21h ; Call MS-DOS to display character

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

pop ax ; Restore ax

ret ; Return from the subroutine

getc:

; Read character into al

push bx ; Save bx

push cx ; Save cx

push dx ; Save dx

mov ah, 1h ; DOS function to read character

int 21h ; Call MS-DOS to read character

pop dx ; Restore dx

pop cx ; Restore cx

pop bx ; Restore bx

ret ; Return from the subroutine

end start

You might also like