MIZAN-TEPI UNIVERSITY
SCHOOL OF COMPUTING AND INFORMATICS
DEPARTMENT OF COMPUTER SCIENCE
INDIVIDUAL ASSIGNMENT OF MICROPROCESSOR AND ASSEMBLEY
LANGUAGE
Course code : CoSe3025
NAME ID
ATSEDE MITIKU 4094/16
Summited to : [Link]
Submsion Date : 29/03/2018 E C
Question 1
[Link] the Structure of 8086 Assembly Program and Clarify all terms.
solution
TITLE <program_name>
.MODEL <memory_model>
.STACK <size>
.DATA
; data definitions (variables, strings, constants)
.CODE
MAIN PROC
; program instructions
MOV AX, @DATA
MOV DS, AX
; main logic here
; exit
MOV AH, 4Ch
INT 21h
MAIN ENDP
END MAIN
Explanation of Each Term
1. TITLE <program_name>
Optional
A label describing the program
Helps programmers identify the file
Example: TITLE Addition Program
1. .MODEL <memory_model>
Defines how code/data are stored in memory.
2. .STACK <size>
Defines stack segment size in bytes.
3. .DATA
Contains all variables, constants, strings.
4. .CODE
1
Starts program instructions.
All CPU operations go here:
MOV
ADD
SUB
JMP
CALL
INT
5. MAIN PROC … MAIN ENDP
Defines a procedure (like a function).
6. Data segment initialization
Before using variables.
7. Program logic
All assembly instructions (calculation, printing, loops, etc.)
8. Program Termination (DOS interrupt)
9. END MAIN
Defines program start address.
Example
TITLE Simple8086
.MODEL SMALL
.STACK 100h
.DATA
msg DB "Hello Assembly!$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; Print text
MOV AH, 09h
MOV DX, OFFSET msg
INT 21h
; Exit
MOV AH, 4Ch
2
INT 21h
MAIN ENDP
END MAIN
Question 2
2. Using LOOP instruction write assembly language program to find out the sum
of following series. 1+2+3+……………………….+110
.MODEL SMALL
.STACK 100H
.DATA
N DW 110
SUM DW 0
MSG DB 'Program to calculate: 1+2+3+...+110',13,10,13,10,'$'
MSG2 DB 'Mathematical check:',13,10,'110 * 111 / 2 = $'
RESULT DB 'Result from loop = $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; Title
3
MOV AH, 09H
LEA DX, MSG
INT 21H
; Using LOOP instruction
MOV CX, [N]
XOR AX, AX
MOV BX, 1
NEXT:
ADD AX, BX
INC BX
LOOP NEXT
MOV SUM, AX
; Display loop result
LEA DX, RESULT
MOV AH, 09H
INT 21H
CALL DISPLAY
; New line
MOV DL, 13
MOV AH, 02H
INT 21H
MOV DL, 10
4
INT 21H
; Mathematical formula
LEA DX, MSG2
MOV AH, 09H
INT 21H
; Calculate: n(n+1)/2
MOV AX, 110
MOV BX, 111
MUL BX
MOV BX, 2
DIV BX
CALL DISPLAY
; Exit
MOV AH, 4CH
INT 21H
MAIN ENDP
; Display AX as decimal
DISPLAY PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV CX, 0
5
MOV BX, 10
DIVIDE_LOOP:
XOR DX, DX
DIV BX
PUSH DX
INC CX
CMP AX, 0
JNE DIVIDE_LOOP
PRINT_LOOP:
POP DX
ADD DL, 30H
MOV AH, 02H
INT 21H
LOOP PRINT_LOOP
POP DX
POP CX
POP BX
POP AX
RET
DISPLAY ENDP
END MAIN
6
Question 3
3. Develop a sequence of instructions that exchange the contents of BX with
AX,CX with DX, and SI with DI.
solution
.model small
.stack 100h
.code
main PROC
xchg ax, bx
xchg cx, dx
xchg si, di
mov ah,4Ch
int 21h
main ENDP
END main
7
Question 4
4. Develop a short sequence of instructions that adds AL, BL, CL, DL and AH.
Save the sum in the DH register.
Solution
MOV DH, AL
ADD DH, BL
ADD DH, CL
ADD DH, DL
ADD DH, AH
Question 5
5. Using jump instruction write assembly language program to find out the sum
of following series.
a. 1+4+7+10+13+……………………… +118
b. 1+2+3+…………………………………. +100
Solution for a.
.model small
.stack 100h
.data
sum dw 0 ; Variable to store the sum (max value will be less than 65535)
8
counter db 1 ; Counter starting from 1, incrementing by 3 each time
msg db 'Sum of series 1+4+7+...+118 is: $'
newline db 13, 10, '$' ; New line characters
.code
main proc
mov ax, @data
mov ds, ax
; Display message
mov ah, 09h
lea dx, msg
int 21h
; Initialize
mov sum, 0
mov counter, 1
calculate_sum:
; Add current term to sum
mov al, counter
mov ah, 0
add sum, ax
; Check if we've reached 118
cmp counter, 118
jge display_result ; If counter >= 118, jump to display
; Increment counter by 3 for next term (1?4?7?...)
add counter, 3
jmp calculate_sum ; Jump back to continue calculation
display_result:
9
; Display newline
mov ah, 09h
lea dx, newline
int 21h
; Convert binary sum to ASCII and display
mov ax, sum
call print_number
; Exit program
mov ah, 4Ch
int 21h
main endp
; Procedure to print a number in AX
print_number proc
mov bx, 10 ; Divisor for base 10
mov cx, 0 ; Counter for digits
convert_loop:
mov dx, 0
div bx ; Divide AX by 10, quotient in AX, remainder in DX
push dx ; Push remainder (digit) onto stack
inc cx ; Increment digit count
cmp ax, 0
jne convert_loop ; Continue if quotient not zero
print_digits:
pop dx ; Pop digit from stack
add dl, '0' ; Convert to ASCII
mov ah, 02h ; Display character function
10
int 21h
loop print_digits
ret
print_number endp
end main
Solution for b.
11
.model small
.stack 100h
.data
sum dw 0 ; Variable to store the sum (1+2+...+100 = 5050)
counter db 1 ; Counter starting from 1, incrementing by 1 each time
msg db 'Sum of series 1+2+3+...+100 is: $'
newline db 13, 10, '$' ; New line characters
.code
main proc
mov ax, @data
mov ds, ax
; Display message
mov ah, 09h
lea dx, msg
int 21h
; Initialize
mov sum, 0
mov counter, 1
calculate_sum:
; Add current term to sum
mov al, counter
mov ah, 0
add sum, ax
; Check if we've reached 100
cmp counter, 100
12
jge display_result ; If counter >= 100, jump to display
; Increment counter by 1 for next term (1?2?3?...)
add counter, 1
jmp calculate_sum ; Jump back to continue calculation
display_result:
; Display newline
mov ah, 09h
lea dx, newline
int 21h
; Convert binary sum to ASCII and display
mov ax, sum
call print_number
; Exit program
mov ah, 4Ch
int 21h
main endp
; Procedure to print a number in AX
print_number proc
mov bx, 10 ; Divisor for base 10
mov cx, 0 ; Counter for digits
convert_loop:
mov dx, 0
div bx ; Divide AX by 10, quotient in AX, remainder in DX
push dx ; Push remainder (digit) onto stack
inc cx ; Increment digit count
13
cmp ax, 0
jne convert_loop ; Continue if quotient not zero
print_digits:
pop dx ; Pop digit from stack
add dl, '0' ; Convert to ASCII
mov ah, 02h ; Display character function
int 21h
loop print_digits
ret
print_number endp
end main
Question 6
14
6. Implement a register exchange between AX and BX using XCHG without MOV.
Solution
; Assign values to AX and BX
MOV AX, 1234H
MOV BX, 5678H
; Exchange AX and BX using XCHG
XCHG AX, BX
Question 7
7. Write 8086 code that compute grade by taking input from user and display letter of
grade on screen
MODEL SMALL
.STACK 100h
.DATA
msgEnter DB "Enter Grade (0-100): $"
msgAplus DB 0Dh,0Ah,"Grade = A+ $"
msgA DB 0Dh,0Ah,"Grade = A $"
msgAminus DB 0Dh,0Ah,"Grade = A- $"
15
msgBplus DB 0Dh,0Ah,"Grade = B+ $"
msgB DB 0Dh,0Ah,"Grade = B $"
msgBminus DB 0Dh,0Ah,"Grade = B- $"
msgC DB 0Dh,0Ah,"Grade = C $"
msgCminus DB 0Dh,0Ah,"Grade = C- $"
msgD DB 0Dh,0Ah,"Grade = D $"
msgF DB 0Dh,0Ah,"Grade = F $"
grade DB ?
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH, 09h
MOV DX, OFFSET msgEnter
INT 21h
MOV AH, 01h
INT 21h
SUB AL, 30h
MOV BL, AL
MOV AH, 01h
INT 21h
16
CMP AL, 0Dh
JE ONE_DIGIT
SUB AL, 30h
MOV BH, AL
MOV AL, BL
MOV CL, 10
MUL CL
ADD AL, BH
MOV grade, AL
JMP CHECK
ONE_DIGIT:
MOV grade, BL
CHECK:
MOV AL, grade
CMP AL, 90
JA SHOW_Aplus
CMP AL, 85
JA SHOW_A
CMP AL, 80
JA SHOW_Aminus
CMP AL, 75
17
JA SHOW_Bplus
CMP AL, 70
JA SHOW_B
CMP AL, 65
JA SHOW_Bminus
CMP AL, 50
JA SHOW_C
CMP AL, 45
JA SHOW_Cminus
CMP AL, 40
JA SHOW_D
JMP SHOW_F
SHOW_Aplus:
MOV DX, OFFSET msgAplus
JMP PRINT
SHOW_A:
MOV DX, OFFSET msgA
JMP PRINT
SHOW_Aminus:
MOV DX, OFFSET msgAminus
JMP PRINT
18
SHOW_Bplus:
MOV DX, OFFSET msgBplus
JMP PRINT
SHOW_B:
MOV DX, OFFSET msgB
JMP PRINT
SHOW_Bminus:
MOV DX, OFFSET msgBminus
JMP PRINT
SHOW_C:
MOV DX, OFFSET msgC
JMP PRINT
SHOW_Cminus:
MOV DX, OFFSET msgCminus
JMP PRINT
SHOW_D:
MOV DX, OFFSET msgD
JMP PRINT
SHOW_F:
MOV DX, OFFSET msgF
PRINT:
19
MOV AH, 09h
INT 21h
JMP EXIT
EXIT:
MOV AH, 4Ch
INT 21h
MAIN ENDP
END MAIN
Question 8
8. Use PUSH and POP to reverse the contents of AX and BX.
Solution
MOV AX, 1234h
20
MOV BX, 5678h
; Reverse AX and BX using PUSH and POP
PUSH AX
PUSH BX
POP AX
; Now AX = 5678h and BX = 1234h
Question 9
9. Write a program to display the content of memory at a given address using MOV and
LEA.
Solution
.model small
.stack 100h
.data
data_byte db 0ABh
msg1 db 'Using LEA: Address=$'
msg2 db ' Content=$'
msg3 db 13,10,'Using MOV: Address=$'
21
msg4 db ' Content=$'
.code
main proc
mov ax,@data
mov ds,ax
; Method 1: LEA
mov ah,09h
lea dx,msg1
int 21h
lea bx,data_byte
call show_address ; Show address
mov ah,09h
lea dx,msg2
int 21h
mov al,[bx]
call show_byte ; Show content
; Method 2: MOV
mov ah,09h
lea dx,msg3
int 21h
22
mov bx,offset data_byte
call show_address
mov ah,09h
mov dx,offset msg4
int 21h
mov al,[bx]
call show_byte
; Exit
mov ah,4ch
int 21h
main endp
; Helper procedures...
show_address proc
; Show BX as hex
push bx
mov cl,4
rol bx,cl
; ... hex display code ...
pop bx
ret
show_address endp
23
show_byte proc
; Show AL as hex
push ax
mov cl,4
rol al,cl
; ... hex display code ...
pop ax
ret
show_byte endp
end main
Question 10
10. Write an 8086 ALP to accept a string and display its length.
Solution
.model small
.stack 100h
.data
24
buffer db 100
msg1 db 'Enter a string: $'
msg2 db 0Dh,0Ah,'Length of the string is: $'
len db 0
.code
start:
mov ax, @data
mov ds, ax
mov ah, 09h
lea dx, msg1
int 21h
mov ah, 0Ah
lea dx, buffer
int 21h
; Calculate length
mov cl, [buffer+1]
mov len, cl
; Display message
mov ah, 09h
lea dx, msg2
int 21h
; Display length as number
mov al, len
add al, 30h
mov dl, al
25
mov ah, 02h
int 21h
mov ah, 4Ch
int 21h
Question 11
11. Write a program to add two 16-bit numbers and store the result in memory.
DATA SEGMENT
NUM1 DW 0bdaeH
NUM2 DW 5647H
RESULT DW ?
CARRY DB 0
MSG1 DB 'Number 1: $'
MSG2 DB 'Number 2: $'
MSG3 DB 'Sum: $'
26
MSG4 DB 'Carry: $'
HEX_OUT DB 4 DUP('0'), 'H$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
MAIN PROC
MOV AX, DATA
MOV DS, AX
; Display first number
MOV AH, 09H
LEA DX, MSG1
INT 21H
MOV AX, NUM1
CALL DISPLAY_HEX
; Display second number
MOV AH, 09H
LEA DX, MSG2
INT 21H
MOV AX, NUM2
CALL DISPLAY_HEX
; Add numbers
MOV AX, NUM1
ADD AX, NUM2
MOV RESULT, AX
27
; Check for carry
JNC NO_CARRY
MOV CARRY, 1
NO_CARRY:
; Display result
MOV AH, 09H
LEA DX, MSG3
INT 21H
MOV AX, RESULT
CALL DISPLAY_HEX
; Display carry flag
MOV AH, 09H
LEA DX, MSG4
INT 21H
MOV DL, CARRY
ADD DL, '0'
MOV AH, 02H
INT 21H
; Exit
MOV AH, 4CH
INT 21H
MAIN ENDP
; Procedure to display AX in hexadecimal
DISPLAY_HEX PROC
MOV CX, 4
28
LEA DI, HEX_OUT
HEX_LOOP:
ROL AX, 4
MOV BL, AL
AND BL, 0FH
CMP BL, 9
JBE HEX_DIGIT
ADD BL, 7
HEX_DIGIT:
ADD BL, 30H
MOV [DI], BL
INC DI
LOOP HEX_LOOP
MOV AH, 09H
LEA DX, HEX_OUT
INT 21H
; Print new line
MOV DL, 0DH
MOV AH, 02H
INT 21H
MOV DL, 0AH
INT 21H
RET
DISPLAY_HEX ENDP
CODE ENDS
29
END
MAIN
Question 12
12. Write an Assembly Language program in which you will add four numbers placed in
AH, AL, BH, and BL register. Store the final sum in CH (high byte) and CL (low byte).
.model small
.stack 100h
.data
msg db 'Result in CH:CL = $'
hex_digits db '0123456789ABCDEF'
.code
30
main proc
mov ax, @data
mov ds, ax
; Display message
mov ah, 09h
lea dx, msg
int 21h
; Initialize registers with sample values
mov ah, 65h
mov al, 34h
mov bh, 56h
mov bl, 78h
; Perform addition
add al, bl
mov cl, al
mov al, ah
adc al, bh
mov ch, al
; Display CH (high byte)
mov al, ch
call display_byte_hex
31
; Display colon
mov dl, ':'
mov ah, 02h
int 21h
; Display CL (low byte)
mov al, cl
call display_byte_hex
; Exit program
mov ah, 4ch
int 21h
main endp
display_byte_hex proc
; Display AL in hexadecimal
push ax
push bx
mov bl, al
; Display high nibble
mov al, bl
shr al, 4
call display_nibble
; Display low nibble
32
mov al, bl
and al, 0Fh
call display_nibble
pop bx
pop ax
ret
display_byte_hex endp
display_nibble proc
; Display 4-bit value in AL
cmp al, 9
jle digit
add al, 7
digit:
add al, 30h
mov dl, al
mov ah, 02h
int 21h
ret
display_nibble endp
33
Question 13
13. Multiply two 8-bit numbers stored in memory using Register Indirect Addressing.
.model small
.stack 100h
.data
numbers db 65h, 13h
product dw ?
msg db 'Product = $'
.code
main proc
mov ax, @data
mov ds, ax
; Display message
mov ah, 09h
lea dx, msg
34
int 21h
; Multiply using indirect addressing
mov si, offset numbers
mov al, [si]
inc si
mov bl, [si]
; Perform multiplication
mov ah, 0
mul bl
mov product, ax
; Display result in hexadecimal
mov bx, ax
call display_word_hex
; Exit program
mov ah, 4ch
int 21h
display_word_hex proc
; Display BX in hexadecimal
push cx
mov ch, 4
35
next_digit:
mov cl, 4
rol bx, cl
mov al, bl
and al, 0Fh
; Convert to ASCII
cmp al, 9
jle digit_disp
add al, 7
digit_disp:
add al, 30h
; Display character
mov dl, al
mov ah, 02h
int 21h
dec ch
jnz next_digit
pop cx
ret
display_word_hex endp
end main
36
Question 14
14. Use CMP and conditional jumps (JE, JNE) to compare two numbers and print which
one is larger.
.model small
.stack 100h
.data
num1 db 89
num2 db 98
msg1 db 'First number (89) is larger$'
msg2 db 'Second number (98) is larger$'
msg3 db 'Numbers are equal$'
.code
main proc
mov ax, @data
mov ds, ax
37
mov al, num1
mov bl, num2
cmp al, bl
jg first_larger
jl second_larger
; equal
mov dx, offset msg3
mov ah, 09h
int 21h
jmp exi
first_larger:
mov dx, offset msg1
mov ah, 09h
int 21h
jmp exit
second_larger:
mov dx, offset msg2
mov ah, 09h
int 21h
exit:
mov ah, 4ch
38
int 21h
main endp
end main
Question 15
15. Perform AND & OR between two memory operands using MOV & AND instructions
indirectly
.model small
.stack 100h
.data
op1 db 0Fh ; First operand (00001111)
op2 db 33h ; Second operand (00110011)
and_result db ?
or_result db ?
msg_and db 'AND Result: $'
msg_or db 13, 10, 'OR Result: $'
39
.code
main proc
mov ax, @data
mov ds, ax
; Display AND message
mov ah, 09h
lea dx, msg_and
int 21h
mov si, offset op1
mov di, offset op2
mov al, [si] ; Load first operand
mov bl, [di] ; Load second operand
; AND operation
and al, bl ; Perform AND
mov and_result, al ; Store result
call display_byte_binary
; Display OR message
mov ah, 09h
lea dx, msg_or
40
int 21h
; Reload original values
mov al, [si]
mov bl, [di]
; OR operation
or al, bl
mov or_result, al
call display_byte_binary
; Exit
mov ah, 4ch
int 21h
display_byte_binary proc
; Display AL in binary
push cx
mov cx, 8
display_bit:
rol al, 1
push ax
jc set_bit
; Display '0'
41
mov dl, '0'
mov ah, 02h
int 21h
jmp next_bit
set_bit:
; Display '1'
mov dl, '1'
mov ah, 02h
int 21h
next_bit:
pop ax
loop display_bit
pop cx
ret
display_byte_binary endp
end main
42
Question 16
16. Write a program to rotate data right through carry (RCR) twice.
.model small
.stack 100h
.data
data_word dw 0A5B4h ; Sample 16-bit data (binary: 1010 0101 1011 0100)
msg_before db 'Before RCR: $'
msg_after db 13, 10, 'After 2x RCR: $'
.code
main proc
mov ax, @data
mov ds, ax
; Display original value
mov ah, 09h
lea dx, msg_before
int 21h
mov bx, data_word
call display_word_binary
; Display message
mov ah, 09h
lea dx, msg_after
int 21h
; Perform rotation
mov ax, data_word
clc
43
rcr ax, 1
rcr ax, 1
; Display result
mov bx, ax
call display_word_binary
; Exit
mov ah, 4ch
int 21h
display_word_binary proc
; Display BX in binary
push cx
push bx
mov cx, 16
display_word_bit:
rol bx, 1
push bx
jc set_word_bit
; Display '0'
mov dl, '0'
mov ah, 02h
int 21h
jmp next_word_bit
set_word_bit:
; Display '1'
mov dl, '1'
44
mov ah, 02h
int 21h
next_word_bit:
pop bx
loop display_word_bit
pop bx
pop cx
ret
display_word_binary end
end main
Question 17
[Link] a program that checks if a number is EVEN or ODD.
Solution
DATA SEGMENT
msgEnter DB 'Enter any number: $'
msgEven DB 0Dh,0Ah,'Number is EVEN $'
msgOdd DB 0Dh,0Ah,'Number is ODD $'
numBuf DB 6
45
DB 0
DB 6 DUP(?)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV DX, OFFSET msgEnter
MOV AH, 09h
INT 21h
MOV DX, OFFSET numBuf
MOV AH, 0Ah
INT 21h
MOV SI, OFFSET numBuf + 2
MOV CL, numBuf + 1
XOR AX, AX
convert_loop:
CMP CL, 0
JE check_even_odd
MOV BL, [SI]
46
SUB BL, 30h
MOV BH, 0
MOV DX, AX
SHL AX, 1
SHL DX, 3
ADD AX, DX
ADD AX, BX
INC SI
DEC CL
JMP convert_loop
check_even_odd:
TEST AL, 1
JZ even_label
odd_label:
MOV DX, OFFSET msgOdd
MOV AH, 09h
INT 21h
JMP exit
even_label:
MOV DX, OFFSET msgEven
MOV AH, 09h
47
INT 21h
exit:
MOV AH, 4Ch
INT 21h
CODE ENDS
END START
48