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

Part 2

This document provides information about assembly language programming concepts for the Real Time and Embedded Systems course at Dilla University. It discusses branch, call, delay, stack instructions and loops. It also covers arithmetic and logic, rotation, and ASCII/BCD conversion instructions. Specifically, it describes unconditional and conditional branch instructions, including the syntax and mnemonics of various conditional branch instructions and examples of comparing numbers and checking flags.

Uploaded by

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

Part 2

This document provides information about assembly language programming concepts for the Real Time and Embedded Systems course at Dilla University. It discusses branch, call, delay, stack instructions and loops. It also covers arithmetic and logic, rotation, and ASCII/BCD conversion instructions. Specifically, it describes unconditional and conditional branch instructions, including the syntax and mnemonics of various conditional branch instructions and examples of comparing numbers and checking flags.

Uploaded by

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

Real Time and Embedded Systems (CoSc 3026)

Laboratory Class
Department of Computer Science
Dilla University

1
Part2-Content
• Assembly language Programming with Branch, call, delay, stack
instructions and loop
• Assembly language Programming with arithmetic and logic, rotation,
instructions, BCD, ASCII conversions
Branch Instructions
• Branch instructions which are supported by the 8086 microprocessor

• There are two types of branches or jumps namely conditional and unconditional branches.

1. Unconditional Branch Instructions


• Instructions in which the PC jumps to the label address provided within the
instruction.
2. Conditional Branch Instructions
• instructions whose execution is based on some condition.
• It checks one or more flag conditions and transfers the control to a new memory
location.
Conditional jumps …Cont..
• The syntax of these instructions is:
Opcode LABEL
• It is a 2-byte instruction consisting of 1-Byte Opcode and 1-byte Label.
• The flag conditions are checked depending upon the instruction,
• if they are true the program control is transferred to the memory address
pointed by the IP register.
• If the condition is not satisfied, then the program continues in a
sequential manner.
Mnemonics of all the conditional branches instructions.
• Table below shows the mnemonics of all the conditional branches instructions.
Instructions Operation Testing Condition
JA/JNBE Jump if Above/Not Below or Equal C=0 and Z=0
JAE/JNB/JNC Jump if Above or Equal/ Jump if Not Below/Jump if No Carry C=0
JB/JNAE Jump if Below/ Jump if Not Above or Equal C=1
JBE/JNA Jump if Below or Equal/ Jump if Not Above (C or Z) =1
JC Jump if Carry C=1
JNC Jump if Not Carry C=0
JCXZ Jump if the CX register=0 CX=0
JE/JZ Jump if Equal/Jump if Zero Z=1
JG/JNLE Jump if Greater/Jump if Not Less Than or Equal ((S xor O) or Z) = 0
JGE/JNL Jump if Greater or Equal/Jump if Not Less Than (S xor O)=0
JL/JNGE Jump if Less Than/Jump if Not Greater Than or Equal (S xor O)=1
JLE/JNG Jump if Less than or Equal/Jump if Not Greater ((S xor O) or Z) = 1
Mnemonics of all the conditional branches instructions…Cont
Instructions Operation Testing Condition
JNE/JNZ Jump if Not Equal/ Jump if Not Zero Z=0
JNP/JPO Jump if Not Parity/Jump if Parity Odd P=0
JNS Jump if Not Signed/Jump if Positive S=0
JO Jump if Overflow O=1
JNO Jump if Not Overflow O=0
JP/JPE Jump if Parity/ Jump if Parity Even P=1
JS Jump if Signed/ Jump if Negative S=1
These instructions are executed after some other instructions which affects the content of
flag registers. Some of them are
• Comparison,
• Addition,
• Subtraction instructions .
CMP Compare Instruction 8086: The compare instruction (CMP)
compares the data of the two operands and depending upon the result
sets the flag. ORG 100h
• The destination operand remains unchanged. .MODEL SMALL
• It compares the two operands by computing the difference of two
.CODE
operands and sets CF, ZF and SF flags.
MOV AX, 0025H ;Sets AX to 25H
MOV BX, 00A5H ;Sets BX to A5H
Format: CMP Destination, Source
CMP AX, BX ;Compare the
contents of AX to BX
• The source and destination can be a register, immediate number, or a RET ;Stops the program
memory address. But, both operands cannot be a memory address in
the same instruction.
• The CMP cannot directly compare the data of two memory addresses • It checks the contents of AX and BX.
• In this example, BX is greater than
AX.
CF ZF SF
• The difference AX-BX sets CF and
Dest=Source 0 1 0 SF to 1 whereas the content of AX
remains unaffected.
Dest>Source 0 0 0
Dest<Source 1 0 1
Exampls
• Assmbly language program that
pushs the smallest value of two
numbers to dx
mov ax,06h
mov bx,04h

cmp ax,bx • Write Assmbly language program that


jb abc pushs the largest value of two numbers to
abc1: dx?
mov dx,bx
hlt

abc:
mov dx,ax
hlt
8
Exampls
• Assmbly language program that
pushs the smallest value of two • Assmbly language program that pushs the
numbers to dx largest value of two numbers to dx
mov ax,06h mov ax,02h
mov bx,04h mov bx,06h
cmp ax,bx
cmp ax,bx ja abc
jb abc abc1:
abc1: mov dx,bx
mov dx,bx
hlt
hlt
abc:
abc: mov dx,ax
mov dx,ax hlt
hlt
9
• E.g. compares two numbers and print if number 1 is equal, greater or less
than number 2. This code is implemented using three conditional branches CMP AH,CH
which are JE, JB and JA. JE L1 ;If AH and CH are equal
• JA/JNBE will check the CF and ZF flags. If both are 0, then the IP will JB L2 ;If AH is less than CH
jump to the target address. Also, if both CF and ZF are equal to 1, then JA L3 ;If AH is greater than CH
the program will continue to execute sequentially. Moreover, if any one of
L1:
them is 0 and other is 1, then this instruction will have no effect of
MOV DX,OFFSET MSG
program execution.
MOV AH,09H
ORG 100h
INT 21H
.MODEL SMALL
RET
.DATA
NUM_1 DB 23H L2:
NUM_2 DB 93H MOV DX,OFFSET MSG1
MSG DB "Equal Numbers$" MOV AH,09H
MSG1 DB "Number 1 is less than Number 2$" INT 21H
MSG2 DB "Number 1 is greater than Number 2$" RET
.CODE L3:
MOV AX, @DATA MOV DX,OFFSET MSG2
MOV DS, AX MOV AH,09H
MOV AH,NUM_1 INT 21H
MOV CH,NUM_2 RET
Cont..
• JA instruction will check if CF is 0.
• If the two numbers are equal, zero flag (ZF) will be equal to 1.
• JE instruction checks ZF.
• If it is 1, the program counter jumps to L1 which will print the message “Equal
numbers” on the emulator screen.
• If the zero flag is 0, then the program counter will jump to the next instruction which is
JB L2. The JB or JBNE instructions check whether CF flag is 1 or not.
• If it is 1, the control is transferred to label address.
• The Compare instruction subtracts the content of CH from AH. If AH is less than the
CH, then it will borrow carry thus setting CF to 1. If CF is 1, then the instructions after
label address L2 will undergo execution.
Check Carry Flag Assembly Example ORG 100h
• The JAE/JNB/JNC instructions check Carry flag .MODEL SMALL
(CF). If it is 0, jump to the target address. .DATA
For example: VAR_1 DB 0C9H
ADD AH, CH VAR_2 DB 7AH
JAE L1 .CODE
• Suppose AH=C9H and BH=7AH. The first instruction MOV AH,VAR_1
adds C9 and 7AH and gives 143. As it generates MOV AL,VAR_2
carry, therefore, CF becomes equal to 1. ADD AH,CH
• Now after the execution of JAE instruction, the JAE L1
program counter will not jump to L1 instead it would ADD AH,AL
execute the next instruction after JAE instruction.
RET
Now replace ADD instruction with CMP instruction.
• If AH is greater or equal to CH data, then the program L1:ADD AL,AH
counter will jump to the L1 label. RET
JO Branch Instruction Assembly Example ORG 100h
• In the e.g, it adds two numbers and check the overflow. .MODEL SMALL
.DATA
• The JO instruction checks the overflow flag. MSG DB "OVERFLOW"
• If the result is too large to fit in the destination register, then NUM_1 DB 23H
it will set overflow bit to 1. NUM_2 DB 93H

• If it is 1, the control will be transferred to NEXT label which .CODE


MOV AX, @DATA
will then display message of “OVERFLOW” on the emulator
MOV DS, AX
screen. MOV AH,NUM_1
• If the OF=0, then the sum is stored in the variable “RESULT” MOV CH,NUM_2
which is stored in memory. ADD AH,CH
• In the e.g the two numbers are 0C9H and 95H. Adding these two JO NEXT
numbers give 15E resulting into overflow. MOV BL,AH
• As OF = 1, therefore after JO instruction, the instructions after RET
label NEXT are executed. NEXT:
• JNO instruction is opposite to JO instruction. MOV DX,OFFSET MSG
MOV AH,09H
• When OF is 0, JNO instruction will jump the program counter INT 21H
to the instruction whose label address is provided within the RET
JNO instruction.
JNP Branch Instruction Assembly Example
.DATA
MSG DB "EVEN PARITY$"
MSG1 DB "ODD PARITY$"
• The JNP instruction checks the parity flag. NUM_1 DB 0C9H
NUM_2 DB 95H
RESULT DB 01 DUP(?)
• If parity is odd or PF=0, the program counter will .CODE
MOV AX, @DATA
jump to the label address. MOV DS, AX

• The JP instruction checks if parity is even or PF=1. MOV AH,NUM_1


MOV CH,NUM_2
ADD AH,CH
• This assembly example checks the parity and shows it JP NEXT
MOV DX,OFFSET MSG1
on emulator screen. MOV AH,09H
INT 21H
RET

ORG 100h NEXT:


.MODEL SMALL MOV DX,OFFSET MSG
MOV AH,09H
INT 21H
RET
JNS Branch Instruction Assembly Example
• JNS is an instruction that is used to check whether the ORG 100h
result is positive or negative and jumps to the label .MODEL SMALL
address depending upon the sign of the result. .DATA
• JNS instruction is a jump if the result of the NUM_1 DW -3C9H
previous instruction is not a signed number or
NUM_2 DW 95H
.CODE
positive MOV AX, @DATA
• JS instruction is a jump if the previous instruction MOV DS, AX
is a signed value or negative. MOV AX,NUM_1
MOV CX,NUM_2
• The code bellow adds two numbers and check if the ADD AX,CX
result is a positive number or not. JS NEXT
• If the number is positive, the program stops RET
running NEXT:
• otherwise the program will jump to the NEXT label MOV AL,-1H
in which the result is multiplied with -1 to convert MUL AX
it into a positive number. RET
LOOP Instruction
• The LOOP instruction provides a simple way to repeat a block of statements a specific number
of times.
• CX is automatically used as a counter and is decremented each time the loop repeats.
• Syntax:
LOOP lable
• The execution of the LOOP instruction involves two steps :
1. First , it subtracts 1 from CX .
2. Next. it compares CX to zero.
• If CX is not equal to zero, a jump is taken to the label identified by destination .
Otherwise,
• if CX equals zero, no jump takes place and control passes to the
instruction following the loop.
Notes:
1. A common programming error is to inadvertently initialize CX to zero before beginning
a loop. If this happens, the LOOP instruction decrements CX to FFFFh, and the
loop repeats 65.536 times .
2. The loop destination must be within -128 to +127 bytes of the current location counter.
Loop
• LOOP Label
Decrease CX, jump to label if CX not zero.

Example:
Use (loop) instruction to find the summation of numbers (1 to 5).

MOV ax, 0 ; Set initial value to ax register


MOV cx, 5 ; Use cx register as a counter
Label:
ADD ax, cx
LOOP label ; Jump to label if cx is not 0
Cont..
• Example, the following code can be used for executing the loop-
body 10 times.
mov CX,10
l1:
DEC CL
loop l1
Hlt
• The JMP instruction can be used for implementing loops. For example, the following code can
be used for executing the loop-body 10 times.

MOV CL, 10
L1:

DEC CL
JNZ L1

18
;Example L1:
; Program to add an array and place its sum in variable ADD AX, [BX];
Result; ADD BX, 2;
.data
INC CX; ; adds '1' to the destination
; operand.
ARRAY DW 1,2,3,4,5
RESULT DW 0 CMP CX, 5
.code JNE L1
mov ax,@data MOV RESULT, AX;
mov ds,ax ;add ax,30
LEA BX, ARRAY ;
MOV CX, 0
MOV AX, 0 mov ah,02h
mov dx ,ax
int 21h
; Code to print in next line
mov ah,02h
;Sample code to display rectangle using * character
mov cx,6 mov dl,0ah ; carriage return
loop2: ; Loop 2 outer loop int 21h
push cx ; you have to push the cx and pop it out before mov ah,02h
;loop instruction to prevent the cx being over written by mov dl,0dh ; line feed
;the inner loop cx
int 21h
mov cx,10
loop1: ; inner loop pop cx
mov ah,02h ; Dos function 02h to Write character to loop loop2
;STDOUT
mov dl,'*' ; character to write
mov ah,4ch ; Dos function to
int 21h ; dos interrupt 21h
;terminate the return to dos
loop loop1
mov al,00 ; return code
int 21h ; dos interrupt 21
Cont…

21
Subroutines:
• Subroutine is a special part of the program that can be called for execution
from any point in the program.
• The subroutine is written to provide a function that must
be performed frequently at various points in the main program.
• Whenever the function must be performed, a single instruction is inserted
into the main body of the program to CALL the subroutine.
• RET instruction must be included at the end of the subroutine to return to the
main program.
• The CALL instruction: calls a procedure
• pushes offset of next instruction on the stack
• copies the address of the called procedure into EIP
• The RET instruction: returns from a procedure pops top of stack into IP
Call and ret instructions
The CALL instruction:
• is used whenever we need to make a call to
some procedure or a subprogram.
• The Syntax for the CALL instruction is as
follows:

CALL subprogram_name

The RET instruction:


• stands for return. This instruction is used at
the end of the procedures or the
subprograms.
• This instruction transfers the execution to the
caller program.
• The Syntax for the RET instruction is as
follows:

RET 23
Example
mov ax, 4;
mov bx,3
call proc1
inc ax
dec bx
Hlt

proc1:
xchg ax, bx
mov cx,6
ret
24
add, inc, dec and sub instructions
• The 8086 provides a variety of arithmetic instructions but you can only carry
out a single arithmetic operation at a time.
• This means that if you wish to evaluate an expression such as: z = x + y + w – v
You will have to use 3 assembly language instructions – one for each arithmetic
operation.  The add instruction adds the source operand to the
• Example destination operand, leaving the result in the
mov ax, 5; load 5 into ax destination operand.
add ax, 3 ; add 3 to the contents of ax,  The destination operand is always the first operand
in 8086 assembly language.
ax now contains 8
 The inc instruction takes one operand and adds 1 to
inc ax ; add 1 to ax, ax now contains 9 it. It is provided because of the frequency of adding 1
dec ax ; subtract 1 from ax, ax now contains 8 to an operand in programming.
sub ax, 6 ; subtract 6 from ax, ax now contains 2  The dec instruction like inc takes one operand and
subtracts 1 from it. This is also a frequent operation
in programming.
 The sub instruction subtracts the source operand
from the destination operand leaving the result in
the destination operand.
• Assembly language Programming with arithmetic and logic, rotation,
instructions, BCD, ASCII conversions
MUL, DIV, Instructions

• MUL Example:
MOV ax, 3; ax=3
MOV bx, 2; bx=2
MUL bx; ax= ax * bx =3*2=6
• DIV Example:
MOV ax, 6; ax=6
MOV bx, 2; bx=2
DIV bx; ax=ax/bx=6/2=3
Example
• Example No.1: Calculate the area of the square shape,
if length A = 10.Notice: Area = A2 = A* A
MOV al, 10d
MUL al
• Example No.2: Calculate the circumference of the
square shape, if length
A= 10. Notice: Circumference = A * 4
MOV al, 10d
MOV bl, 4d
MUL bl
Questions
• Question No. 1: Calculate the area of the rectangle,
if length A= 10 & width B = 5. Notice: Area = A * B.

• Question No.2: Calculate the circumference of the


rectangle, if length A=10
& width B =5. Notice: Circumference = 2 * (A + B).
Questions
• Solution Question No. 1 : Calculate the area of the
rectangle, if length A= 10 & width B = 5. Notice: Area = A *
B.
MOV al, 10d
MOV bl, 5d
MUL bl
Sol=50d or 32h
• Solution Question No. 2 : Calculate the circumference of
the rectangle, if length A=10
& width B =5. Notice: Circumference = 2 * (A + B).
MOV al, 10d
MOV bl, 5d
ADD al, bl
MOV cl, 2
MUL cl
AND: instruction perform logical AND operation between
two operands.
• source can be an immediate, register, or a memory • The AND operation between these
location two numbers give: 1001 1000 0000
• destination can be either a register or a memory • The hexadecimal value of 1001
location. Both source and destination operands cannot 1000 0000 is 980.
be a memory location. ORG 100h
• It ANDs each bit of source operand with the destination .MODEL SMALL
operand and stores the result back into the destination .CODE
operand. MOV BX,3527
• content of the source operand remains unchanged. MOV CX,2968
• If both input bits are 1, only then AND operation will
AND BX,CX
generate 1 as an output bit.
Format : AND Destination, Source RET
• The code is given below. The two
E,g: Suppose two numbers 3527 and 2968. numbers are stored in BX and CX.
The final result obtained after AND
Binary of 3527: 1101 1100 0111 operation goes to the BX register.
Binary of 2968: 1011 1001 1000
OR Logical Instruction
• It performs the OR operation between two operands and
stores the result back into the destination operand. ORG 100h
• destination operand can be a register or a memory .MODEL SMALL
location
.DATA
• source can be immediate, register, or a memory
VAR_1 DW 3527
location.
• But Keep in mind, both operands should not be a .CODE
memory location. MOV AX, 0700h
MOV DS, AX
• The OR instruction clears the CF and OF flags to 0 and
MOV CX, 2968
update PF, ZF, and SF flags.
OR CX, [102h]
• The OR operation gives 1 at output if any one input is 1 RET
and give 0 on output only when both inputs are 0.
• The code given performs the OR
Format : OR Destination, Source
operation between contents of
• E.g. Suppose two numbers 3527 and 2968. The OR CX register and the data stored
operation between these two numbers give: 4063 at offset address 102h.
• The hexadecimal value of 4063 is FDF.
XOR Instruction 8086
• This instruction performs the XOR operation between bits
of source and destination operands.
• The XOR operation gives 1 when both inputs are different.
When both inputs are same then the output will be zero.
ORG 100h
• source operand can be either a register or memory
address .MODEL SMALL
• destination operand can be immediate, register or .CODE
memory location. MOV AH,68H
Format: XOR Destination, Source
XOR AH,9CH
• XOR Logic Example: Suppose AH is loaded with 68H. The
XOR instruction performs the XOR operation between the RET
contents of AH and the immediate value of 9CH.
• The XOR operation between these two numbers gives the
hexadecimal value of F4H.
Logical Instructions (AND, OR, XOR , and NOT) Examples

• AND operand1, opernad2


Example:
MOV al, 110b; al=6
MOV bl, 101b; bl=5
AND al, bl; al= 4

• OR operand1, opernad2
Example:
MOV al, 110b; al=6
MOV bl, 101b; bl=5
OR al, bl; al= 7
Cont..
• XOR operand1, number2
Example:
MOV al, 110b; al=6
MOV bl, 101b; bl=5
XOR al, bl; al= 3

• NOT operand1
Example:
MOV al, 11111010b; al=FAh
NOT al; al=5
e.g. Suppose AX is equal to 029AH
Shift Left Instruction: There are two shift left instructions. and count is 2. Then
• One is used for signed operands Binary value of Operand 029A =
• other one is used for unsigned operands. 0010 1001 1010
Format: SHL Destination, Count 1st shift: 0101 0011 0100 CF=0
• SHL is the logical left shift for unsigned operands. 2nd shift: 1010 0110 1000 CF=1
• It shifts the operand bits to the left. The hexadecimal value of 1010 0110
• The destination can be a register or a memory location. 1000 is A68H.
• The number of shifts are stored in CL register which is ORG 100h
then loaded in the count operand. .MODEL SMALL
• If the number of shifts are 1, then you can directly .CODE
specify it in the instructions like SHL Destination, 1. MOV CL, 2
• After shifting, the MSB goes to CF flag register and MOV AX,029AH
LSB is filled with zeros.
SHL AX,CL
RET
SHL/SHR

• SHL/SHR operand, number


Example:
MOV ax, 8; ax=8d=1000b
SHL ax, 1; ax=16d=10000b
SHR ax, 2; ax=4d=100b
Cont..
• ROL operand1, operand2
• Shift all bits left, the bit that goes off is set to CF and the same bit is
inserted to the right-most position.
Example:
MOV AL, 1Ch ; AL = 00011100b
ROL AL, 1 ; AL = 00111000b

• ROR operand1, operand2


• Shift all bits right, the bit that goes off is set to CF and the same bit is
inserted to the left-most position.
Example:
MOV AL, 1Ch ; AL = 00011100b
ROR AL, 1 ; AL = 00001110b
Example:
What is the value of (Al) register after executing the following
instructions?
MOV al, 10001100b ; al=8Ch
ROL al, 2 ; al=32h
ROR al, 3 ; al=46h
Homework
1. AX=02456h
BX=5866
CX=7895
a. Write assembly language program to push the largest value to DX
b. Write assembly language program to push the smallest value to DX
2. Write an assembly code that prints the numbers from 1 to 5, 5 times on the screen. Each
sequence of numbers from 1 to 5 are separated by new line.
12345
12345
12345
12345
12345

3. AX=4d
BX=3d
a. Write assembly language program to push the sum to CX and difference To DX using call instruction(subroutine)
4. Write assembly language program that calculates sum, difference, product and division of the given numbers
and push the result to stack using call instruction(subroutine)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy