NAME: HOORAIN SAJJAD
ROLL NUMBER: BM-21020
COURSE TITLE: MICROPROCESSOR
PROGRAMING AND ITS INTERFACING
COURSE CODE: CS-430
COURSE TEACHER: DR. SAAD QASIM KHAN
Third Year; Biomedical Engineering; Fall Semester 2023
Assignment
Attempt any five of the following questions:
1. Write a program that takes two numbers as input and performs their addition. The
code for addition of the numbers should be present in another assembly file that
should be called as a near procedure in the first file.
2. Write a program, which
1. Loads AX, BX, CX and DX registers with A154, 7812, 9067, BFD3.
2. Exchange lower byte of CX and higher byte of DX registers by using
memory location 0150 in between the transfer. Then stores CX and DX
registers onto memory location 0170 onward.
3. Exchange higher byte of AX and higher byte of BX registers by using
memory location 0160 in between the transfer. Then stores AX and BX
registers onto memory location 0174 onward.
4. Also draw the flow chart of the program.
3. Write a program that produces certain delay and then increment the Accumulator
register. When accumulator produces a carry then the buzzer should generate tone for
a certain time. Implement this program using subroutine. The length of delay is
passed to the delay subroutine as a parameter, using stack. Also draw the flowchart.
You can also use any assembler for this exercise.
4. Write a program, which multiply two 8-bit numbers using add and shift logic. Check
the program by
I. loads accumulator with 20H and then multiply it by 10H.
II. loads BL with 10H and multiply it by 12H.
Use any assembler of your choice for this purpose. Also draw the flow chart of the
program.
5. Write a program, which prints your name on the screen when ‘space’ key is pressed
from the keyboard. Implement using conditional jump instruction. Also draw the
flow chart of the program.
6. Write a program, which will add the contents of two 32 bit numbers stored in DX –
AX (DX contains the high order word) and memory location WORD PTR [0202] –
WORD PTR [0200].
7. Write a program, which calculate the factorial of any given number (the number
may be used as an immediate operand in the instruction). Use any assembler for this
exercise.
QUESTION 1
Write a program that takes two numbers as input and performs their addition.
The code for addition of the numbers should be present in another assembly file
that should be called as a near procedure in the first file.
EXTRN A: NEAR
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "ENTER FIRST NUMBER: $"
MSG2 DB 0DH, 0AH, "ENTER SECOND NUMBER: $"
.CODE
MAIN PROC
; To move the data defined under .DATA to addresses of actual DS
MOV AX,@DATA
MOV DS, AX
; Display message
LEA DX, MSG1 ; pass offset address of message to DX register
MOV AH, 09H
INT 21H
; User input, which will be stored in AL register in ASCII code
MOV AH, 01H
INT 21H
; Subtract to make the entered number usable in its original value
SUB AL, 48H
MOV BL, AL ; store 1st entered number in variable NUM1
; Display second message
LEA DX, MSG2
MOV AH, 09H
INT 21H
; User input for second operand, which will be stored in AL register in ASCII code
MOV AH, 01H
INT 21H
; Subtract to make the entered number usable in its original with
SUB AL, 48H
MOV BH, AL
CALL A
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Near procedure, containing addition program.
PUBLIC A
.MODEL SMALL
.DATA
MSG DB 0DH, 0AH,"SUM IS: $"
CHAR DB ?,'$'
.CODE
A PROC NEAR
LEA DX, MSG
MOV AH, 09H
INT 21H
ADD BH, BL
ADD BH, 48H
MOV DL, BH
MOV AH, 02H
INT 21H
MOV AH, 4CH
INT 21H
RET
A ENDP
END
QUESTION 2
Write a program, which:
I. Loads AX, BX, CX and DX registers with A154, 7812, 9067, BFD3.
II.Exchange lower byte of CX and higher byte of DX registers by using
memory location 0150 in between the transfer. Then stores CX and DX
registers onto memory location 0170 onward.
III. Exchange higher byte of AX and higher byte of BX registers by using
memory location 0160 in between the transfer. Then stores AX and BX
registers onto memory location 0174 onward.
IV. Also draw the flow chart of the program.
.MODEL SMALL
.DATA
.STACK 100H
; DEFINE VARIABLES
A DW 0A154H
B DW 7812H
C DW 9067H
D DW 0BFD3H
.CODE
MAIN:
; INITIALIZE THE STACK SEGMENT
MOV AX, @DATA
MOV DS, AX
MOV SS, AX
MOV SP, 100H ; SET STACK POINTER TO THE TOP OF THE STACK SEGMENT
; LOAD VALUES INTO AX, BX, CX, AND DX REGISTERS
MOV AX, A
MOV BX, B
MOV CX, C
MOV DX, D
; EXCHANGE LOWER BYTE OF CX AND HIGHER BYTE OF DX USING MEMORY
LOCATION 0150
MOV SI, 0150H
MOV [SI], CL
MOV CL, DH
MOV DH, [SI]
; STORE CX AND DX ONTO MEMORY LOCATION 0170 ONWARD
MOV SI, 0170H
MOV [SI], CX
MOV [SI+2], DX
; EXCHANGE HIGHER BYTE OF AX AND HIGHER BYTE OF BX USING MEMORY
LOCATION 0160
MOV SI, 0160H
MOV [SI], AH
MOV AH, BH
MOV BH, [SI]
; STORE AX AND BX ONTO MEMORY LOCATION 0174 ONWARD
MOV SI, 0174H
MOV [SI], AX
MOV [SI+2], BX
; EXIT THE PROGRAM
MOV AH, 4CH
INT 21H
END MAIN
FLOW CHART
START
LOAD REGISTERS
AX, BX, CX AND DX
EXCHANGE BYTES OF CX
AND DX REGISTERS THEN
STORE THEM IN MEMORY
LOCATION 0170 ONWARDS
EXCHANGE BYTES OF AX
AND BX REGISTERS THEN
STORE THEM IN MEMORY
LOCATION 0174 ONWARDS
STOP
EXIT
QUESTION 3
Write a program that produces certain delay and then increment the
Accumulator register. When accumulator produces a carry then the buzzer
should generate tone for a certain time. Implement this program using
subroutine. The length of delay is passed to the delay subroutine as a parameter,
using stack. Also draw the flowchart. You can also use any assembler for this
exercise.
.MODEL SMALL
.STACK 100H
.DATA
DELAY_COUNT EQU 1 ; Adjust the delay count for the desired duration
BUZZER_STATE DB 0 ; 0 - Buzzer off, 1 - Buzzer on
.CODE
DELAY_SUBROUTINE PROC
DELAY_LOOP:
LOOP DELAY_LOOP
RET
DELAY_SUBROUTINE ENDP
TOGGLE_BUZZER PROC
POP CX ; Restore the original CX value
CMP BUZZER_STATE, 0
JE BUZZER_OFF
TOGGLE_BUZZER ENDP
MOV DX, 1046H
MOV AX, 0001H ; Non-zero value for ON
; Adjust port address as needed
OUT DX, AX
MOV AX, 0000H
JMP DISPLAY_AND_EXIT
; Turn off the buzzer
BUZZER_OFF:
MOV DX, 1046H
MOV AX, 0000H ; Zero value for OFF
; Adjust port address as needed
OUT DX, AX
DISPLAY_AND_EXIT:
RET
MAIN PROC
MOV AX, 0FFFAH
ABC:
ADD AX, 1
JC XYZ
; Additional delay before looping
MOV CX, DELAY_COUNT
CALL DELAY_SUBROUTINE
JMP ABC
; Turn on the buzzer
XYZ:
MOV BUZZER_STATE, 1
CALL TOGGLE_BUZZER
; Simulated buzzer tone delay
MOV CX, 1 ; Adjust the count for the desired duration
CALL DELAY_SUBROUTINE
; Turn off the buzzer
MOV BUZZER_STATE, 0
CALL TOGGLE_BUZZER
; Exit DOS and end program
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
FLOW CHART
START
INITIALIZE AX
ABC LOOP
AX += 1
CARRY
SET FLAG NOT SET
DELAY LOOP
XYZ
TOGGLE
BUZZER BUZZER IS
OFF
BUZZER IS
ON
EXIT
QUESTION 4:
Write a program, which multiply two 8-bit numbers using add and shift logic.
Check the program by:
I. Loads accumulator with 20H and then multiply it by 10H.
II. Loads BL with 10H and multiply it by 12H.
Use any assembler of your choice for this purpose. Also draw the flow chart of
the program.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
; (a) apply multiply logic using add and shift logics
MOV AX, 20H
MOV CL, 4
SHL AX, CL
; (b) second multiplication
MOV BL, 10H
MOV CL, 2
SHL BL, CL
MOV DL, BL
SHL BL, 1
ADD BL, DL
Exit DOS and end program
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
FLOW CHART:
START
LOAD AX WITH 20H
AND CX WITH 4
MULTIPLY AX BY 10H USING
SHIFT OPERATION AND STORE
RESULT IN AX
LOAD BL WITH 10H
AND CL WITH 2
MULTIPLY BL BY 12H USING
SHIFT AND ADD OPERATIONS
AND STORE RESULT IN BL
EXIT
STOP
QUESTION 6:
Write a program, which will add the contents of two 32 bit numbers stored in DX –
AX (DX contains the high order word) and memory location WORD PTR [0202] –
WORD PTR [0200].
.MODEL SMALL
.STACK 100H
.CODE
.STARTUP
; Add lower bytes
MOV BX, WORD PTR [0200]
ADD AX, BX
MOV BX, WORD PTR [0202]
ADC DX, BX
; Exit DOS and end program
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN