Professional Documents
Culture Documents
CHAPTER 4 Robotics
CHAPTER 4 Robotics
CHAPTER 4 Robotics
• For completing the task of the assembly, assembler needs some hints from the
programmer i.e. the required storage of particular constant or variable,
logical names of the segments, types of different subroutines or modules, end
of file etc.
• Another type of hint which helps the assembler to assign a particular constant
with a label or initialize particular memory locations or labels with
constants is an operator.
2
Assembler directives are specific for a
particular assembler
However all the popular assemblers like
the Intel 8086 macro assembler, the
turbo assembler and the IBM macro
assembler use common assembler
directives
3
8086 Microprocessor Assembler Directives
General form:
Segnam SEGMENT
…
… Program code
… or
… Data Defining Statements
…
…
Segnam ENDS
Assume
• Used to tell the assembler the names of the logical
segments to be assumed for different segments
used in program.
• You must tell the assembler that what to assume for any
segment you use in the program.
• Example, ASSUME CS: CODE tells the assembler that
the instructions for the program are in segment named
CODE, and hence the CS register is to be loaded with
the segment address allotted by the operating system
for label CODE.
• ASSUME DS:DATA indicates that data items related to
the program are available in logical segment named
DATA.
Assembler Directives(contd.)
Another Format
.CODE: This directive provides shortcut in definition of
the code segment.
General format for this directive is as shown below
.CODE[name]
The name is optional. It is basically specified to
distinguish different code segments, when there are
multiple code segments in the program.
.DATA: This directive provides shortcut in definition of
the data segment.
Naming Data and addresses
8
Assemble Directives ---for variables
DB, DW, DD, DQ, DT: These directives are used to define
different types of variables, or to set aside one or more
storage locations of corresponding data type in memory
Their definitions are as follows:
DB – Define Byte … used to declare a byte type variable or to
store a byte in memory location.
DW – Define Word … used to define a variable of type word or to
reserve storage location of type word in memory
DD - Define Double word--- used to declare a variable of type
double word
DQ – Define Quad words --- used to declare a variable 4 words in
length
DT – Define Ten Bytes --- used to declare a variable which is 10
bytes in length. 9
DUP
• Allows a sequence of storage locations to be defined or
reserved . If you need to declare a large array you can
use DUP operator. It also assigns/intializes values to
this location
• The syntax for DUP:
format: Name Data_Type number DUP ( values )
• number - number of duplicate to make (any constant
value).
• value - expression that DUP will duplicate.
• More compactly as
message DB ’Bye’,0DH,0AH
13
14
8086 Microprocessor
Assemble Directives DW
DD
Define Word
15
Arrays
17
Equal Sign Directive
• name = expression
– expression must be numeric
– these symbols may be redefined at any
time
maxint = 7FFFh
count = 1
DW count
count = count * 2
DW count
EQU Directive
EQU – Equate
• Used to give a name to some value or to a symbol.
• It is just to reduce the recurrence of the numerical
values or constants in a program code
• Format: name EQU expression
– expression can be string or numeric
– Use < and > to specify a string EQU
– these symbols cannot be redefined later in the
program
sample EQU 7Fh;assigns 7FH with label
sample
aString EQU <1.234>
message EQU <This is a message>
Assembler Directives and Operators
• LABEL: label
– The Label directive is used to assign a name to the current
content of the location counter. When the assembly process
starts, the assembler initializes a location counter to keep track
of memory location assigned to the program.
20
Assembler Directives(contd.)
Example:
• OFFSET
– It is an operator which tells the assembler to determine the offset or
displacement of a named data item(variable) from the start of the segment
which contains it.
DATA SEGMENTS
LIST DB 10H
DATA ENDS
CODE SEGMENT
MOV SI, OFFSET LIST ; this is the same as, LEA SI, LIST
CODE ENDS
23
Assembler Directives and Operators
SEG: Segment
– The segment operator returns the segment part of a label or
variable’s address.
– For example: for the definition below
DATA SEGMENT
NUM1 DB 0F2H
NUM2 DB 2EH
DATA ENDS
.MODEL
– Provides short-cuts in defining segments.
$
26
Assembly Language Example Programs
Write an assembly language program to transfer a block of bytes
from one memory location to another memory location by using
string instructions
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STR1 DB 'COMMUNICATION ENGINEERING'
LEN EQU $-STR
STR2 DB LEN DUP(0)
DATA ENDS
CODE SEGMENT
START:MOV AX,@DATA
MOV DS, AX
MOV SI, OFFSET STR1; LEA SI, STR1
LEA DI, STR2 ; MOV DI, OFFSET STR2
MOV CX, LEN
BACK: MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
DEC CX
JNZ BACK
HLT
CODE ENDS
END START
27
Assembly Language Example Programs
The above program can be again written in another format as follows
o In the above program replace the statements, DEC CX & JNZ BACK with
LOOP BACK, and it becomes as follows.
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STR1 DB 'COMMUNICATION ENGINEERING'
LEN EQU $-STR
STR2 DB LEN DUP(0)
DATA ENDS
CODE SEGMENT
START:MOV AX,@DATA
MOV DS, AX
MOV SI, OFFSET STR1; LEA SI, STR1
LEA DI, STR2 ; MOV DI, OFFSET STR2
MOV CX, LEN
BACK: MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
LOOP BACK
HLT
CODE ENDS
END START
28
Assembly Language Example Programs
The above program can be again written in another format as
follows
.MODEL SMALL
.DATA
STR1 DB 'COMMUNICATION ENGINEERING'
LEN EQU $-STR
STR2 DB LEN DUP(?)
.CODE
START:MOV AX,@DATA
MOV DS, AX
MOV SI, OFFSET STR1; LEA SI, STR1
LEA DI, STR2 ; MOV DI, OFFSET STR2
MOV CX, LEN
BACK: MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
LOOP BACK
HLT
END START
29
Assembly Language Example Programs
The above program can be again written in another format as
follows
.MODEL SMALL
.DATA
STR1 DB 'COMMUNICATION ENGINEERING'
LEN EQU $-STR
STR2 DB LEN DUP(?)
.CODE
START:MOV AX,@DATA
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET STR1; LEA SI, STR1
LEA DI, STR2 ; MOV DI, OFFSET STR2
MOV CX, LEN
CLD
BACK: MOVSB
LOOP BACK
HLT
END START
30
Assembly Language Example Programs
The above program can be again written in another format as follows
o By using the prefix instruction, REP, you can remove the LOOP
BACK statement
.MODEL SMALL
.DATA
STR1 DB 'COMMUNICATION ENGINEERING'
LEN EQU $-STR
STR2 DB LEN DUP(?)
.CODE
START:MOV AX,@DATA
MOV DS, AX
MOV AX, 2000H
MOV ES, AX
MOV SI, OFFSET STR1; LEA SI, STR1
LEA DI, STR2 ; MOV DI, OFFSET STR2
MOV CX, LEN
CLD
REP MOVSB
HLT
END START
31
Assembly Language Example Programs
The above program can be again written in another format as follows
o In the above program, ES and DS were equals, i.e., They are on
the same segment. But, DS and ES can also have different
values as follows.
.MODEL SMALL
.DATA
STR1 DB 'COMMUNICATION ENGINEERING'
LEN EQU $-STR
STR2 DB LEN DUP(?)
.CODE
START:MOV AX,@DATA
MOV DS, AX
MOV AX, 2000H
MOV ES, AX
MOV SI, OFFSET STR1; LEA SI, STR1
LEA DI, STR2 ; MOV DI, OFFSET STR2
MOV CX, LEN
CLD
REP MOVSB
HLT
END START
32
Assembly Language Example Programs
33
Assembly Language Example Programs
• An alternative method for the program of addition of two numbers is
given below:-
• Please note that in this method there is no need to have two different
segments of memory.
• In next few slides we will discuss few assembly language programs. 34
Assembly Language Example Programs
35
Assembler Directives and Operators
ORG: Originate
– This directive directs the assembler to start the memory
allocation for a particular segment (data, code, or stack)
form the declared offset address in the ORG statement.
– While starting the assembly process for a memory segment,
the assembler initializes a location counter (LC) to keep track
of the allotted offset addresses for the segment.
– When the ORG directive is not mentioned , LC is initialized
with the offset address 0000h. When the ORG directive is
mentioned at the beginning of the statement, LC is initialized
with the offset address specified in the ORG directive.
36
Assembler Directives and Operators
ORG – Originate
– The ORG statement changes the starting offset address of the data.
– When the ORG directive is not mentioned , LC is initialized with the
offset address 0000h.
– When the ORG directive is mentioned at the beginning of the
statement, LC is initialized with the offset address specified in the
ORG directive.
– Example:
• ORG 100h
o When this directive is placed at the beginning of the data
segment, the next data storage starts from the offset address
0100h within the data segment.
o If it is placed in the code segment, the location counter is
initialized with 0100h and the first instruction is stored from
37
the offset address 0100h within the code segment.
8086 Microprocessor
Assemble Directives
EXAMPLES
38
Assembler Directives and Operators
• PUBLIC
– The PUBLIC directive is used along with the Extern directive.
• This informs the assembler that the labels, variables constants, or
procedures declared PUBLIC may be accessed by other assembly
modules to form their codes, but while using the PUBLIC declared
labels, variables, constants or procedures, the user must declare
them externals using the extern directive.
• EXTERN: External
– The directive EXTERN informs the assembler that the names,
procedures and labels declared after this directive have
already been defined in some other assembly language
modules.
39
Assembler Directives and Operators
• GLOBAL
– The labels, variables, constants or procedures declared GLOBAL may
be used by other modules of the program. Once a variable is declared
GLOBAL, it can be used by any module in the program.
• LOCAL
– The labels, variables, constants or procedures declared LOCAL in a
module are to be used only by the particular module.
– LOCAL a, b, DATA, ARRAY, ROUTIN
41
Assembler Directives and Operators
Example:
• INC [BX] ; This instruction will not know
whether to increment the byte pointed to
by BX or a word pointed to by BX.
• INC BYTE PTR [BX] ;increment the
byte ;pointed to by BX
• This PTR operator can also be used to
override the declared type of variable . If
we want to access a byte in an array
• WORDS DW 437Ah, 0B97h,
• MOV AL, BYTE PTR WORDS
42
Assembler Directives and Operators
• FAR PTR
– This directive indicates the assembler that the label following
FAR PTR is not available within the same segment and the
address of the label is of 32- bits i.e. 2-bytes offset followed
two byte segment address.
• NEAR PTR
– This directive indicates that the label following NEAR PTR is
in the same segment and needs only 16-bit i.e. 2-byte offset to
address it.
• SHORT
– The SHORT operator indicates to the assembler that only one
byte is required to code the displacement for a jump (i.e.
displacement is within -128 to +127 bytes from address of the
byte next to the jump opcode).
• JMP SHORT LABEL 43
8086 Microprocessor
Assemble Directives
procname ENDP
44
8086 Microprocessor
Assemble Directives
Examples:
RET
ADD64 ENDP
RET
CONVERT ENDP
45
8086 Microprocessor
Assemble Directives
46
Assembly Language Example Programs
47
MACRO
• Macro is a group of instructions.
• The macro assembler generates the code in the program
each time where the macro is ‘called’.
• Macro can be defined by MACRO & ENDM assembler
directives.
49
MACRO
• Passing Parameters in Macro
o In macro, parameters are passed as part of statement which
calls Macro.
51
Timings and Delays
• In the real time applications, it is important to keep
track with time.
• Real Time Applications:
Traffic Light Control, Digital Clock,
Process Control, Serial Communication
etc.
For example in traffic light control application, it is
necessary to give time delays between two
transitions.
• These time delays are in few seconds
Can be generated with the help of executing group
of instructions number of times.
52
Timings and Delays … Cont’d
• Timer Delays Using NOP Instruction
NOP instruction does nothing but takes 3 clock
cycles of processor time to execute.
So, by executing NOP instruction in between two
instructions we can get delay of 3 clock cycles.
• Timer Delay using Counters
Counting can create time delays.
Since the execution times of the instructions used in
counting routine are known, the initial value of the
counter, required to get specific time delay can be
determined.
53
Timings and Delays … Cont’d
• Timer Delay using Counters … Cont’d
56
Timings and Delays … Cont’d
• Timer Delay using Counters … Cont’d
57
Timings and Delays … Cont’d
• Timer Delay using Counters … Cont’d
58
Timings and Delays … Cont’d
• Timer Delay using Nested Loops
In this program, one more external loop is
added to execute the internal loop multiple
times.
The inner loop is nothing but the program we
have seen in the last section.
59
Timings and Delays … Cont’d
• Timer Delay using Nested Loops … Cont’d
60
Timings and Delays … Cont’d
61
Timings and Delays … Cont’d
62
Assembly Language programming
• A program is a set of instructions arranged in the
specific sequence to do the specific task.
• It tells the microprocessor what it has to do.
• The process of writing the set of instructions which
tells the microprocessor what to do is called
“Programming”.
• In other words, we can say that programming is the
process of telling the processor exactly how to
solve a problem.
• To do this, the programmer must “speak” to the
processor in a language which processor can
understand.
63
Assembly Language programming
Steps involved in Programming
• Specify the problem
• Designing the problem-solution
• Coding
• Debugging
o Once the program is coded, the next step is
debugging the code.
o Debugging is the process of testing the code
to see if it does the given task.
o If program is not working properly,
debugging process helps in finding and
correcting errors.
64
Assembly Language programming
Flow Chart
• To develop the programming logic programmer
has to write down various actions which are to
be performed in proper sequence.
• The flow chart is a graphical tool that allows the
programmer to represent various actions which
are to be performed.
o The graphical representation is very useful
for clear understanding of the programming
logic.
65
Assembly Language programming
Flow Chart
• This figure shows the graphic symbols used in
the flow chart
Graphical symbols used in flow chart
66
Assembly Language programming
Sample Flow Chart
67
Assembly Language Programming
Assembly Language programs
• Machine Level Language Programs
o a program which has simply a sequence of the binary
codes for the instructions
o this binary form of the program is referred to as
machine language because it is the form required by
the machine.
• However, to write program in machine language,
programmer has to memorize the thousands of
binary codes for a processor.
o This task is difficult and error prone.
• To make programming easier, usually programmers
write programs in assembly language.
o then, they translate the assembly language program
to machine language so that it can be loaded into 68
memory and executed.
Assembly Language Programming
Assembly Language programs
• Assembly language uses two, three or four letter
words to represent each instruction types.
o These words are referred to as mnemonics.
• The letters in an assembly language mnemonics are
usually initials or a shortened form of English
word(s) for operation performed by the instruction.
o For Example:
Mnemonic for additions is ADD,
The mnemonic for logic AND operation is AND,
and
The mnemonic for instruction copy data from one
location to another is MOV.
69
Assembly Language Programming
Assembly Language programs
• Gives you a better appreciation of how the
hardware works than high-level languages
• Resources are given by the programmer’s model
• These include registers, hardware resources in
CPU, and memory used for data storage.
• Look through the instructions in category, find the
mnemonic for the required operation, then find
the correct addressing mode.
The Assembler
• Converts (assembles) a source file into binary
codes (machine codes) executed by the computer.
o 1-to-1 correspondence between assembly
language statements and the machine code in
memory 70
Assembly Language Programming
Assembly Language programs
71
Programming with an Assembler
• Steps
Lets us see the steps involved in developing and
executing assembly language programs.
72
Programming with an Assembler
Steps
• The first step in the development process is to write
an assembly language program.
• The assembly language program can be written with
an ordinary text editor such as word star, edit,
emu8086 editor and so on.
• The assembly language program text is an input to
the assembler.
o The assembler translates assembly language
statements to their binary equivalents, usually
known as object code.
o Time required to translate assembly code to object
code is called assemble time.
• During assembling process assembler checks for
syntax errors and displays them before giving object
code module. 73
Programming with an Assembler
Steps
• The object code module contains information about
where the program or muddle to be loaded in
memory.
• If the object module is to be linked with other
separately assembled modules then it contains
additional linkage information.
• At link time, separately assembled modules are
combined into one single load module, by the linker.
• The linker also adds any required initialization or
finalization code to allow the operating system to start
the program running and to return control to the
operating system after the program has completed.
• At load time, the program loader copies the program
into the computer’s main memory, and at execution
time, program execution begins.
74
Programming with an Assembler
Steps
• Assembling process
o As mentioned earlier, assembler translates a source file that
was created using the editor into machine language such as
binary or object code.
o The assembler reads the source file of our program from
the disk where we saved it after editing.
• Linking process
o A linker is a program used to join together several object
file.
o When writing large programs, it is usually much more
efficient to divide the large program into smaller modules.
Each module can be individually written, tested and
debugged.
When all the modules work, they can be linked together
to form a large functioning program.
75
Programming with an Assembler
• Linking process … cont’d
o The linker produces a link file which contains the
binary codes for all the combined modules.
• Debugging Process
o A debugger is a program which allows us to load our
object code program into system memory, execute the
program, and debug it.
o How does a debugger help in debugging a program?
1. the debugger allows to look the contents of registers
and memory locations after our program runs.
2. It allows us to change the contents of register and
memory locations and return the program
3. Some debugger allows us to stop execution after each
instruction so we can check or alter memory and
register contents.
4. Etc…
76
Assembly Language Example Programs
• Please compare these programs with lab manual II
77
Assembly Language Example Programs
• Please compare these programs with lab manual II
78