0% found this document useful (0 votes)
4 views9 pages

04 Microprocessor Programs

The document provides an overview of microprocessor programming, focusing on assembly language and its structure, including examples of assembly programs and the use of labels and comments. It explains the process of converting assembly code into machine code through assembling, as well as the use of pseudo-opcodes and flowcharts in program development. Additionally, it covers looping and string instructions, demonstrating how to implement various operations in assembly language.

Uploaded by

simonmagz25
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)
4 views9 pages

04 Microprocessor Programs

The document provides an overview of microprocessor programming, focusing on assembly language and its structure, including examples of assembly programs and the use of labels and comments. It explains the process of converting assembly code into machine code through assembling, as well as the use of pseudo-opcodes and flowcharts in program development. Additionally, it covers looping and string instructions, demonstrating how to implement various operations in assembly language.

Uploaded by

simonmagz25
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

Microprocessor Programs

In order for a microprocessor to carry out a task, it needs a program to be written and entered into the memory. Programs are
developed using some programming language after which they are converted into sequences of binary-coded instructions. A
program made up of binary codes is known as a machine code. A program written using some programming language is known
as a source code. The lowest programming language is the assembly programming language. Assembly language programs
shall be used to enable a deeper understanding of how microprocessors work.

Assembly Programs
An assembly program is essentially a sequence of assembly instructions listed in such a way to perform the required task. Each
instruction is placed on its own line.

Example 1
The following is an assembly program that reads a number from a port, adds the number 7Fh to it and stores the result in
memory location pointed to by register BX.

IN AL,05h
ADD AL,7FH
MOV [BX],AL
HLT

The HLT instruction is used to ensure that the program does not wonder off.

Example 2
The following is an assembly program that calculates power given current and voltage. Voltage value is stored in register BX
and current value is stored in register CX. The power value is required to be stored in memory starting at address 1800h.

MOV AX,BX
MUL CX
MOV [1800h],AX
MOV [1802h],DX
HLT

Labels
A label is a symbolic name for the memory address at which the instruction is located in memory. Labels are useful when
writing programs that branch. A jump to a particular instruction is represented as a jump to the label at which the instruction is
located. In 8086 assembly programs, labels are indicated by placing a colon after the label.

Example 3
The following is a program to turn on or off street lights depending on light intensity. The light intensity is provided as an
unsigned 8-bit number by a suitable sensor connected to port at address 08h. The light darkness threshold value is C5h. There
are 8 street lights each one connected to each pin on port at address 09h. Turning on a lamp is achieved by driving high the
port pin connected to the lamp.

START: IN AL,08h
CMP AL,0C5h
JAE LIGHT_OFF
MOV AL,0FFH
OUT 0Ah,AL
JMP START
LIGHT_OFF: MOV AL,00H
OUT 0Ah,AL
JMP START

27
The program reads the light intensity value through port 08h and compares it to the threshold value. Depending on results of
the comparison, the program then switches on or off the street lights. Selective turning on and off of lights is achieved using
conditional branch instructions. If the light intensity is below the threshold, the program branches to instructions that turn off
the lamps; else, the program proceeds to execute instructions that turn on the lamps. START and LIGHT_OFF are labels that
allow the programmer to use them as references to points where the program is desired to branch to.

Comments
Comments are text statements that are usually used to improve readability of the assembly program by a human being. A
comment is indicated by preceding the comment text with a semi-colon and ends at the end of the line. The program from
example 3 is shown next with comments added.

START: IN AL,08h ; read light intensity


CMP AL,0C5h ; compare to threshold value
JAE LIGHT_OFF ; branch to switch off lights
MOV AL,0FFH ; code to switch on lights
OUT 0Ah,AL ; turn off lights
JMP START ; check intensity again
LIGHT_OFF: MOV AL,00H ; code to switch on lights
OUT 0Ah,AL ; switch on lights
JMP START ; check intensity again

Use of Flowcharts in Developing Programs


For complicated problems, flowcharts help simplify the development of the program. The algorithm can be developed in the
form of a flowchart and then the flowchart is turned into a program.

Example 4
Figure 4.1 shows a flowchart for a program to copy a block of data from one block of locations to the other (copy & paste).
Assume source block starts at 4000h and destination block starts at 8000h and 1000h items are to be copied. The commented
program is shown after the flowchart.

Start

SI = 4000h
DI = 8000h
CX = 1000h

MOVE_NEXT

Move contents of memory pointed by SI


to memory pointed to by DI

Increment SI
Increment DI

Decrement CX

N
CX = 0?

End

Figure 4.1 Flowchart for the copy-paste program.

28
The copy-paste program is shown next:

START: MOV SI,4000H ; source block starting address


MOV DI,8000H ; destination block starting address
MOV CX,1000H ; number of items to be moved
MOVE_NEXT: MOV AX,[SI] ; move from source to AX
MOV [DI],AX ; move from AX to destination
INC SI ; increment source pointer
INC DI ; increment destination pointer
DEC CX ; decrement counter
JNZ MOVE_NEXT ; repeat till counter is zero
HALT

When a counting program uses CX as the counter, the 8086 also supports JCXZ, a conditional jump that makes program branch
to some position based on the value in CX.

Assembling Programs
Assembly programs must be changed into machine code before the program can be executed. The process of changing assembly
programs into machine code is called assembling. It involves replacing each assembly instruction with the corresponding
machine code. Assembling can be done by hand using a look up table or using a computer program called an assembler.

For manual assembling, each instruction is replaced with corresponding binary code obtained from the look up table. The
binary codes are then placed in sequential memory locations. An assembling sheet is usually used for manual assembling. A
sample of the sheet is given in Appendix A. Note that when assembling 16-bit immediate, address and displacement values,
the lower byte is stored in the lower address (i.e. lower address).

For assembling using an assembler program, the program is entered into text editor following a format that the assembler
understands: at least one space between instruction fields, colons to indicate labels, semicolons to indicate comments, etc. The
assembler program is then run and it converts the assembly program into machine code (at a faster rate than a human being).

Machine code for Example 2


Suppose the program is to be loaded from address 0201h. The machine code will appear as shown in table 4.1.

Table 4.1 Example 2 machine code


Assembly Memory Address Machine code
Instruction (Hex)
MOV AX,BX 8201 8B
0202 C3
MUL CX 0203 F7
0204 29
MOV [1800h],AX 0205 89
0206 06
0207 01
0208 18
MOV [1802h],DX 0209 89
020A 16
020B 02
020C 18
HLT 0204 F4

Pseudo-opcodes
Pseudo-opcodes are symbols that are used to give instructions to be followed during the process of assembling a program.
Typical pseudo-opcodes are:
 ORG - tells the assembler where to start putting the machine code into the memory, e.g., ORG 8000H tells the
assembler to start loading the next program codes or data starting at location 8000h.

29
 EQU – assigns a symbolic name to a value. Equate statements are used to make programs more readable, by
incorporating names instead of numbers. For example, TTY EQU 5H means wherever the symbol TTY is encountered,
replace it with the number 5.
 DB – tells assembler to load a series of bytes in memory during the programming phase. It is usually employed where
look up tables need to be loaded into memory during programming.

Given next is example 1 re-written with pseudo-opcodes incorporated.

bias equ 7Fh

ORG 0800h

IN AL,05h ; read from port 05h into accumulator


ADD AL,bias ; add bias (7h) to value in accumulator
MOV [BX],AL ; store result in memory pointed to by BX
HLT ; stop program

The ORG statement tells the assembler the code is to be loaded starting at address 0850h. Wherever bias appears, it has to be
replaced with the number 7Fh. The corresponding machine code is shown in Table 4.3.

Table 4.3 Example 1 machine code


Assembly Memory Machine code (Hex)
Instruction Address
IN AX,05h 0800 E4
0801 05
ADD AL,bias 0802 14
0803 05
MOV [BX],AL 0804 8A
0805 07
HLT 0806 F4

Example Use of DB statement


The DB and ORG statements can be used to load some data values into memory at time of programming, for example a look
up table. Given next is an example application to store seven segment codes for the hexadecimal digits in a memory block
starting at address D800h.

ORG D800h

DB seg_ FCh,60h,DAh,F2h,66h,B6h,BEh,E0h,FEh,F6h,E6h,3Eh,9Ch,7Ah,9Eh,8Eh

Assembling Labels
Labels that form operands in assembly instructions need to be converted into memory addresses. The program is first assembled
with blank spaces left for the 16-bit or 8-bit addresses/displacements corresponding to the label values. For absolute jumps, the
addresses of labeled instructions are read off from the assembly sheet and filled into each blank memory that should contain a
label. For relative jumps (conditional jumps are relative jumps), the offset of the labeled instruction from the position of the
jump instruction is calculated and filled into each blank memory that should contain a label. The offset is an 8-bit signed number
in 2’s complement format. Positive numbers are for jumping forward while negative numbers are for jumping backwards.

Assembling Example 4 – with pseudo codes added


The following is a modified copy & paste program to include the ORG statement. The corresponding machine code is shown
in Table 4.4.

ORG 3050H

START: MOV SI,4000H ; source block starting address


MOV DI,8000H ; destination block start address
MOV CX,1000H ; number of items to be moved
MOVE_NEXT: MOV AX,[SI] ; move from source to A
30
MOV [DI],AX ; move from A to destination
INC SI ; increment source pointer
INC DI ; increment destination pointer
DEC CX ; decrement counter
JNZ MOVE_NEXT ; repeat till counter is zero
HALT

Table 4.4 Example 4 machine code


Label Assembly Instruction Memory Machine
Address code (Hex)
MOV SI,4000H 3050 C7
3051 C6
3052 00
3053 40
MOV DI,8000H 3054 C7
3055 C7
3056 00
3057 80
MOV CX,1000H 3058 C7
3059 C1
305A 00
305B 10
MOVE_NEXT MOV AX,[SI] 305C 8B
305D 04
MOV [DI],AX 305E 89
305F 05
INC SI 3060 FF
3061 C6
INC DI 3062 FF
3063 C7
DEC CX 3064 FF
3065 C9
JNZ MOVE_NEXT 3066 75
3067 AB
HLT 3068 F4

More on Programs and Instructions


Loop Operations
The 8086 has the looping instructions LOOP, LOOPZ and LOOPNZ. They allow program execution to loop back to a labelled
instruction for a number of times, i.e. allow a block of code to be executed repeatedly a number of times. A counter register is
used to store the number of times that the loop is executed. The counter is automatically decremented each time the loop is
executed until it becomes zero. The counter register is CX.

Example 5 –copy-paste using loop instructions


START: MOV SI,4000H ; source block starting address
MOV DI,8000H ; destination block starting address
MOV CX,1000H ; number of items to be moved
MOVE_NEXT: MOV AX,[SI] ; move from source to AX
SUB AX,CX ; subtract item position from value
MOV [DI],AX ; move from AX to destination
INC SI ; increment source pointer
INC DI ; increment destination pointer
LOOP MOVE_NEXT ; loop back till counter is zero
31
HALT

String Instructions
The 8086 supports the string instructions MOVS, LODS, STOS, CMPS and SCAS. They are used to perform the same operation
on a successive number of items (strings). They use index registers SI and DI to point to the items being worked on. The index
registers automatically increment/decrement each time the chain instruction executes.

CLD and STD are instructions used to respectively clear and set the control flag DF. DF defines whether SI and/or DI post-
decrement or post-increment in chain instruction executions. Post-increment occurs if DF is reset and post-decrement occurs if
DF is set.

Example 6 – copy paste using string instructions


START: MOV SI,4000H ; source block starting address
MOV DI,8000H ; destination block starting address
MOV CX,1000H ; number of items to be moved
CLD ; make SI and DI increment in chain instructions
MOVE_NEXT: MOVSW ; move word from memory pointed to by SI to
; memory pointed to by DI
; increment source and destination pointers
LOOP MOVE_NEXT ; loop back till counter is zero
HALT

Note that in the x86 Assembly format, the registers used (8-bit or 16-bit for the 8086) tell the data width of the operands; where
there is ambiguity of data widths, suffixes may be added to the opcode mnemonic to indicate the operand width for example
MOVW – move word.

Repeat Operations
Repeat operations of particular instructions are achieved using instruction prefixes REP, PEPZ, REPNZ. They allow string
instructions to be executed a number of times. The number of times is stored in the counter register CX.

Example 7 –copy paste with repeat instructions


MOV SI,4000H ; source block starting address
MOV DI,8000H ; destination block starting address
MOV CX,1000H ; number of items to be moved
CLD ; make SI and DI increment in chain instructions
REP MOVSW ; move from memory pointed to by SI to memory pointed
; to by DI. Increment source and destination pointers
; Repeat till counter is zero
HALT ; stop program

Assembling Prefixes
The prefix code is added before the instruction opcode. For example, REP MOWSW becomes F3A5.

Intersegment Jumps
The instruction to be branched to is specified by a segment selector and an offset. The same opcode is used and the assembler
uses the right jump instruction.

Note that in x86 microprocessors, the memory addresses specified by instructions are the offsets of operands from the base of
the corresponding segment. The complete address of an item (referred to as its logical address) is specified using the segment
selector and the offset. In assembly language and where needed it is expressed in the format SEGMENT SELECTOR:OFFSET
is used for example 0500h:9800h; DS:856h.

Change of Segments
Segments are changed by loading new values into the segment registers. Ordinary move operations are used. These allow
movement of data from a GPR to a segment register, memory to a segment register, segment register to GPR and segment
register to memory.

32
High Level Language Programming
Nowadays, high level language is used to program computers (microprocessors). High level language consists of English-like
statements and is easier to comprehend as well as less tedious to use than low level programming language.

Once a program has been written in high level language and debugged, it is converted into machine language by a program
called a compiler. The process is called compiling.

Example 8
The following is a C programming language statement where input integers are 8-bit values in memory at addresses w, x and
y. The result is stored at memory location z (can potentially be 16 bits wide). Convert it into an equivalent 8086 assembly
language program. Hence compile the program.
int x,y,z;
z = y*w - x;

SOLUTION
w equ 202h
x equ 202h
y equ 204h
z equ 206h

MOV AL,[w] ; load value w into AL


MOV BL,[y] ; load value y into BL
MUL BL ; multiply value w and y
MOV BL,[x] ; load value x into BL
MOV BH,00h ; clear value in BH
SUB AX,BX ; subtract BX from AX
MOV [z],AX ; load value x into memory

The Design Process for Microprocessor Programs


Design of microprocessor based solutions generally involves the following steps:
1. Understand the problem: form of data involved, necessary interface circuits, existing ways of solving situation at hand.
2. Develop algorithm: step by step statements or a flowchart.
3. Write program: covert algorithm into a program using a suitable programming language.
4. Assemble/compile the program, load into memory and run to test to debug the program. Testing and debugging may
run for a number of iterations before a perfect solution is realized.

Review Questions
1. What is an assembly program?
2. What is machine code?
3. What is a label? Of what use is it? How is a label indicated?
4. What is a comment? How is a comment indicated?
5. What fields make up an assembly language statement? What is the use of each field?
6. What is assembling? How is it carried out?
7. What are pseudo codes? Of what use are they?
8. Describe the various programming constructs.
9. Describe the steps involved in program design.
10. What is compiling as applied to computer programs?

Chapter 4 Tutorial Questions


1. A machine is turned on by writing the ASCII codes for the letters in “NUST” one after the other
to port 04H. Write an assembly language program to turn on the machine using a microprocessor
system. Assemble the program.

33
2. Write an assembly language program to compare the numbers in memory locations 7076H and
7077H and put the larger of the two in memory location 7078H. Assemble the program.

3. Write an assembly language program to read value on port 09H, do the following:
 If it is larger than 0028H, write 00000011b to port 0AH.
 If it is smaller than 28H but larger than 0024H, write 00001100b to port 0AH.
 If it is smaller than 0024H, write 00110000b to port 0AH.

Assemble the program.

4. The contents of a block of 100h memory locations starting at address 0300h is to be moved to a
memory black that starts at address 500h. To each item from the source block, the constant 30h
is to be added so that the corresponding memory location in the destination block contains a
value equal to the value in the source block plus 30h. Write a program to carry out the operation.
Assemble the program.

5. Write an assembly program to add the decimal numbers 1 through to 16 and put the result in
memory location 2050h. Use a loop to produce a short program. Assemble the program.

6. Write an assembly language program to decrement a register pair to zero 1000 times and toggle
state of port 0Dh each time zero is reached. Assemble the program.

7. The following are typical high level program statements:


a) x = u + v;
b) y = u – v;
u and v are values in memory; x and y are the results that need to be stored in the memory. Write
an equivalent assembly language program for each statement. Assemble the program.

8. The following are typical high level program statements:


a) x = u + v - 35;
b) y = 3u – 2w;
c) y = u + 5*v – u/w;
u, v, w, x and y are values in memory locations. Write an equivalent assembly language program
for each statement. Assemble the program.

9. The following is a typical high level program segment


if (u<v) u = v;
y = u - 7;
u, v and y are values in memory locations. Write an equivalent assembly language program for
the above program. Assemble the program.

Program Design Questions


Question 1
Develop a program that finds the maximum in a block of locations from memory address 5001H to memory
address 5200H. The numbers are unsigned values.

34
Repeat for finding the minimum.

Question 2
A microprocessor based system that works as follows is required:
 When temperature is below 20˚C, turn on a heater
 If temperature is in the range 20˚C to 28˚C, switch off everything
 If temperature is above 28˚C, turn on a fan

The fan is connected to pin 0 and the heater to pin 1 on port 05h. The temperature is provided by an ADC
connected between the temperature sensor and port address 04h. The ADC has a resolution of 0.25 ˚C.

Design the system.

Question 3
Write a program to move characters in memory block addresses 7001H to 8000H to memory block starting at
C000. The source block contains a text body. When moving it to the new block, any lower case characters must
be converted to uppercase.

Question 4
Develop the following delay routines. You need to first research on how to achieve delays using register
decrement loops.
 1 second delay
 5 seconds delay
 10 seconds delay

35

You might also like