100% found this document useful (1 vote)
56 views40 pages

CH-06 8086 Assembly

ch 6

Uploaded by

Ermias Lemesa
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
100% found this document useful (1 vote)
56 views40 pages

CH-06 8086 Assembly

ch 6

Uploaded by

Ermias Lemesa
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/ 40

Microcomputers and Interfacing

(ECEg-4161)

8086 Assembly Language Programming

Lecture 06

Beyene Jember UoG iOT Department of ECE


Introduction
 A program is nothing but set of Instructions written sequentially and stored in
computers memory for execution by microprocessor.
 Program can be written in 3 levels
 Machine Language
 Consists of individual instructions that will be executed by the CPU one at a
time.
 Binary codes for the instructions to be executed by microcomputer
 Assembly Language (Low Level Language)
 Designed for a specific family of processors (different processor
groups/family has different Assembly Language)
 Consists of symbolic instructions directly related to machine language
instructions one-for-one and are assembled into machine language.
 High Level Languages
 e.g. : C, C++ and Vbasic
Introduction…
 Assemble, Link and execute Program
Introduction…

 Assembler
 An assembler is a program that converts source-code programs written in
assembly language into object files in machine language.
 Popular assemblers include MASM, NASM, GNU EMU8086 etc.
Introduction…
 Linker
 A linker program combines your program's object file created by the assembler
with other object files and link libraries, producing a single executable
program.
 You need a linker utility to produce executable files.
8086 Assembly language
 Each personal computer has a microprocessor that manages the
computer's arithmetical, logical, and control activities.
 Each family of processors has its own set of instructions for handling various. These
set of instructions are called 'machine language instructions'.
 The low-level assembly language is designed for a specific family of processors that
represents various instructions in symbolic code and a more understandable form.
 Advantages of Assembly Language
• It requires less memory and execution time;
• It allows hardware-specific complex jobs in an easier way;
• It is suitable for time-critical jobs;
• It is most suitable for writing interrupt service routines and other memory
resident programs.
8086 Assembly language…
 Assembly language instructions equate one-to-one to machine-level instructions, except they
use a mnemonic to assist the memory.
 Each instruction is represented by one assembly language statement.
 There may be only one statement/line.
 A statement may start in any column.
 A statement is either
• An 8086 instruction: Executable statements that actually do something or
• An Assembler directive: provides directions to the assembler program.

 A statement is composed of a name, a mnemonic, operands and an optional


comment.
Format of 8086 Instruction
 General format for an assembly language statement

[Name] Mnemonic [Operand1 [ ,Operand2] ] [;Comment]

Example:
Start: MOV AX, BX ; copy BX into AX
INC CX ; increment CX
RET ; return from procedure
 The entities enclosed with in square brackets are optional.
 Start is a user defined name and you only put in a label in your statement when
necessary!.
 The symbol : is used to indicate that it is a label.
 The symbol ; is used to indicate that it is a comment.
Format of 8086 Instruction...
 Names/labels Field
 Used for instruction labels, procedure names, and variable names.

 Assembler translates names into memory addresses

 Names are 1-31 characters including letters, numbers and special characters ? .
@_$%

 Names may not begin with a digit

 If a period is used, it must be first character

 Case insensitive

 Cannot use reserved names.


Format of 8086 Instruction
 Names/labels Field…
 Can be used to name variables. Such when put in front of a memory allocation directive. Can
also be used to define a constant.
• For example:

count1 db 50 ; a variable (memory allocation directive db)

count2 equ 100 ; a constant

 Can be used as labels for statements to act as place markers to indicate where to jump to.
Can identify a blank line.
• For example:

label1: MOV AX,10


MOV BX,0
.

label2:

JMP label1 ; jump to label1


Format of an Assembler directive
 Mnemonic field
 Identifies an instruction or directive.
 Standard keywords of the assembly language for a particular processor
 Instruction

 Symbolic operation code (opcode)


 Symbolic opcode translated into machine language opcode
 Describes operation’s function; e.g. MOV, ADD, SUB, INC.
 Assembler directive
 Contains pseudo-operation code (pseudo-op)
 Pseudo-ops qualify Name as segment name, macro-name and so on.
 Not translated into machine code
 Tell the assembler to do something.
Format of an Assembler directive
 Operands field
 Specifies data to be acted on
 Operands may be a register, a variable, a memory location ,an immediate value or
input/output port.
 Zero, one, or two operands

 Comments field
 Any text can be written after the statement as long as it is preceded by the “;”.
 A semicolon marks the beginning of a comment
 A semicolon in beginning of a line makes it all a comment line
 Good programming practice dictates comment on every line
Assembler directives (Preprocessors)
 Assembler directives (pseudo-instructions) give directions to the assembler about how it
should translate the assembly language instructions into machine code.
 It provides information to assist the assembler in producing executable code.
 It creates storage for a variable and initialize it.
 Assembler directives (pseudo-instructions) give directions to the assembler about how it should
translate the assembly language instructions into machine code.
 Assembler directives are specific for a particular assembler.
 Used to :
 Specify the start and end of a program
 Attach value to variables
 Allocate storage locations to input/ output data
 Define start and end of segments, procedures, macros etc..
Assembler directives …

 The ORG (Origin) Directive


 is used to assign the starting address (Effective address) for a program/ data segment.
 Example:
 ORG 1000H ; Informs the assembler that the statements following ORG 1000H should
be stored in memory starting with effective address 1000H

 The END Directive


 Tells the assembler to stop reading instruction or statements that are written after END
directive.
 Is used to terminate a program;
 Statements after END will be ignored
Assembler directives …
 Segment Directive
 The SEGMENT directive identifies the start of a memory segment and ENDS identifies
the end of a segment when full-segment definitions are in use.
Syntax:
<logical-segment_name> SEGMENT
..
..
<logical-segment_name> ENDS
 E.g.
mySegment SEGMENT
MOV AX,BX
..
..
mySegment ENDS
Assembler directives …
 Assume Directive
 The ASSUME statement tells the assembler what names have been chosen for the code,
data, extra, and stack segments.
 Tells which logical segment to use for each of the physical segment at a given time.
 The ASSUME statement is only used with full-segment definitions
 Syntax:
ASSUME <Physical-Segment> : <logical-segment-name>

 Example
ASSUME CS: ACODE, DS:ADATA ;Tells the compiler that the instructions of the program are stored in
the segment ACODE and data are stored in the segment ADATA
Assembler directives …
 EXTRN
 Used to tell the assembler that the names or labels following the directive are in
some other assembly module.
 GLOBAL
• Be used in place of PUBLIC or EXTRN directives. It is used to make the symbol
available to other modules. INCLUDE Include Source Code from File
• Example: INCLUDE “program_2.asm”
 LENGTH
• Is an operator, which tells the assembler to determine the number of elements
in some named data item, such as a string or an array.
• Example: MOV CX, LENGTH string1 CX = no of elements in string1
Assembler directives …
 NAME
 Used to give a specific name to each assembly module when programs consisting of
several modules are written.
 Example: NAME “Main”

 OFFSET
 Is an operator which tells the assembler to determine the offset or the displacement of a
named data item (variable) or procedure from start of the segment which contains it.
 Example: MOV BX, OFFSET TABLE
Assembler directives …
 PROC
 Indicates the beginning of a procedure
 ENDP
 End of procedure
 FAR
 Intersegment call
 NEAR
 Intra-segment call
Assembler directives …
 MACRO
 Indicate the beginning of a macro
 ENDM
 End of a macro
 General form:
Assembler directives …
 Defining Data in a program
 Data is usually stored in the data segment.
 You can define constants, work areas (a chunk of memory).
 Data can be defined in different lengths (8-bit, 16-bit)
 Each byte of character is stored as its ASCII value in hexadecimal
The definition for data:
<Name> DX <Expression>

 Name – name of data segment.


 DX – this is called the directives. It defines length of the data.
 Expression – defines the value (content) of the data segment.
Assembler directives …
 Defining Data in a program…
 Pseudo-op Description Data size (in bytes)
DB Define Byte 1
DW Define Word 2
DD Define Double word 4
DQ Define Quad-word 8
DT Define Ten bytes 10
 Examples:
var DW 1234H ;define var as word and initialize with value 1234 (in Hex).
var DB ? ;define var as Byte and its initial value is unknown.
Assembler directives …
 Defining Arrays
 Initialized Array:
array1 DB ‘a’, ’b’, 2 ;initialized array of 3 elements, size of each element is 1 byte
array2 DB 20 dup(5) ;array2 is byte array of 20 elements all initialized to value 5
 Uninitialized Array:
array3 DB 20 dup(?) ;array3 is uninitialized byte array of 20 elements

 DUP – duplicate can be used to define multiple storages.


 arr stores the first value, arr + 1 stores the second value and so on.
• E.g Mov AL, [arr+1] ; [] used for dereferencing, store value at arr+1 in AL
Assembler directives …
 Named constant Directive
 EQU

 This directive does not define a data item; instead, it defines a value that the assembler
can use to substitute in other instructions (similar to defining a constant in C
programming or using the #define )
FACT EQU 12 ;defines FACT as a named constant
MOV CX, FACT ;without dereferencing
 No memory is allocated.
 Strings are also possible.
Assembler directives …
 User defined Data definition directives
 Structure:
Struct myStruct ;declares myStruct as a structure
var1 DB 0 ; Var1 data byte initialized with 0
var2 DB 1 ; Var2 data byte initialized with 1
Ends myStruct
 Structure variable:
structVar myStruct ? ;creates structure variable
 Acceccing structure:
MOV [structVar.var1], 20 ;move 20 in var1 in mystruct
Program Examples
Example 1
Determine the contents of the GP registers and flags after these programs.

ORG 40H
MOV AX,1234H JMP START JMP START
MOV BX,5678H V DB 5, 8, 12, 1 NAME DB “Abebe”
PUSH AX START: START:
PUSH BX MOV AL, 0 MOV SI, OFFSET NAME
POP AX MOV BX, 0 INC SI
POP BX MOV DL, 24 LOADS [SI]
DO: ADD AL, V[BX] MOV CX, 05H
INC BX REPEAT: INC SI
AX ? CMP AL, DL CMP AL, [SI]
BX ? JLE DO LOOPE REPEAT

AL ? DL ? BX ? IP? AL? CX? [SI]? ZF?


Standard I/O
 Interrupt instruction
 Interrupts can be seen as a number of functions.
 These functions make the programming much easier, instead of writing a code to print a
character you can simply call the interrupt and it will do
everything for you.
 We call such functions software interrupts.
 To make a software interrupt there is an INT instruction, it has very simple syntax:
 INT type
 Type: a number between 0 and 255 which identifies the interrupt.
 Where value can be a number between 0 to 255 (or 0 to 0FFh)
 Each interrupt may have up to 256 sub-functions (so we get 256*256 = 65536 functions)
 To specify a sub-function AH register should be set before calling interrupt.
Standard I/O
 Interrupt instruction…

• CPU pushes current program address and status in to stack before executing the ISR.
• In DOS, INT 21 is used for basic I/O functions, like display, print…
 DOS function calls are used for Standard input/output in Assembly language(8086).
 To use a DOS function call in a DOS program,
1. Place the function number in AH (8 bit register) and other data that might be necessary
in other registers.
2. Once everything is loaded, execute the INT 21H instruction to perform the task.
 After execution of a DOS function, it may return results in some specific registers.
Standard I/O (CONT..)
 INT 21h /01H: Read from the Keyboard
 Read character from standard input, result is stored in AL(in ASCII ).
 If there is no character in the keyboard buffer, the function waits until any key is
pressed.

Example: MOV AH,01H ;Load DOS function number in AH


INT 21H ;Access DOS
;Returns with AL = ASCII key code
Standard I/O …
 INT 21h/02H: Write to Standard Output device
 This function displays single character to standard output.
 ASCII key code of the character to be displayed must be loaded in DL register.

Example:
MOV DL, ‘A’ ;load ASCII key code of Character ‘A’ in DL
MOV AH,02H ;load DOS function number in AH
INT 21H ;access DOS
Standard I/O …
 INT 21h/09H: Display a character String
 This function displays a character string on the standard output.
 The character string must end with an ASCII of symbol ‘$’ (24H).
 The character string can be of any length and may contain control characters such as
carriage return (0DH) and line feed (0AH).
 DX must contain address of the character string.
 Example:
Buf DB “Hello World$” ;define character string
MOV DX, offset Buf ;load address of the string in DX, offset gives address of the Buf.
MOV AH,09H ;load DOS function number in AH
INT 21H ;access DOS
Standard I/O …
 INT 21h/0AH: Buffered keyboard input
 This function continues to read the keyboard (displaying data as typed) until either the
specified number of characters are typed or until the enter key is typed.
 The first byte of the buffer contains the size of the buffer (up to 255).
 The second byte is filled with the number of characters typed upon return.
 The third byte through the end of the buffer contains the character string typed, followed
by a carriage return (0DH).
Buffer e.g: Buf DB 13, 10, “Welcome$”
Example:
Buf DB 10, ?, 10 dup(0) ;declare a buffer.
MOV DX, offset Buf ;load address of the buffer in DX, offset gives address of the Buf.
MOV AH,0AH ;load DOS function number in AH
INT 21H ;access DOS
Standard I/O …
mov al, 5 ; 05h, or 00000101b
mov bl, 10 ; 0Ah, or 00001010b
add bl, al ; 5 + 10 = 15 (decimal), 0Fh,
or 00001111b
; print result in binary ;Print the string “hello world”
mov cx, 8 ;for the 8 bits msg db "hello world “, 0DH, 0A, “$“
mov dx, offset msg
print: mov ah, 2 ; print function mov ah, 9
mov dl, '0‘ int 21h
test bl, 10000000b ; test first bit. ;wait for any key press
jz zero mov ah, 1
mov dl, '1‘ int 21h
zero: int 21h ;print to console ret ;return to the operating system
shl bl, 1
loop print
; print binary suffix:
mov dl, ‘b’
int 21h
N.B
Carriage Return, (ASCII 0DH) is the control character to bring the cursor to the start of a line.
Line-feed (ASCII 0AH) is the control character that brings the cursor down to the next line on the screen
Procedure and MACRO
 While writing programs, there might be a case where a particular sequence of instructions is
used several times through out a program .
 To avoid writing the sequence of instructions again and again in the program, the same
sequence can be written as a separate subprogram:
 Use separate procedure
 Use Macro
 Procedure/Subroutines
 Write it in a separate subprogram and call that subprogram whenever necessary.
 Avoid writing the same sequence of instruction again and again.
 CALL instruction transfers the execution control to procedure.
 In the procedure, a RET instruction is used at the end.
 This will cause the execution to be transferred to caller program.

Procedure/Subroutines
 At the time of invoking a procedure the address of the next instruction of the program is kept on
the stack so that, once the flow of the program has been transferred and the procedure is done,
one can return to the next line of the original program, the one which called the procedure.
 Intra-segment procedure(IP in stack)
 Intersegment procedure(CS:IP in stack
 The PROC and ENDP directives indicate the start and end of a procedure (subroutine).
 Both the PROC and ENDP directives require a label to indicate the name of the procedure.
Syntax:
<Procedure-name> PROC [NEAR|FAR] ;starts procedure
.. ;body of the procedure
..
RET
<Procedure-name>ENDP ;close of the procedure

 NEAR | FAR is optional and gives the types of procedure. If not used, assembler assumes the
procedure as near procedure. All procedures are defined in code segment. The directive ENDP
indicates end of a procedure.
Procedure/Subroutines
Procedure/Subroutines
 Advantages:
• Programming becomes simple.
• Reduced development time as each module can be implemented by different
persons.
• Debugging of smaller programs and procedures is easy.
• Reuse of procedures is possible.
• A library of procedures can be created to use and distribute.
 Disadvantages:
• Extra code may be required to integrate procedures.
• Liking of procedures may be required.
• Processor needs to do extra work to save status of current procedure and load
status of called procedure. The queue must be emptied so that instructions of the
procedure can be filled in the queue.
Macros
 A macro is a group of instructions that perform one task, just as a procedure
performs one task.
 The difference is that a procedure is accessed via a CALL instruction, whereas a
macro, and all the instructions defined in the macro, is inserted in the program at
the point of usage.
 Creating a macro is very similar to creating a new opcode, which is actually a
sequence of instructions, in this case, that can be used in the program.
 You type the name of the macro and any parameters associated with it, and the
assembler then inserts them into the program.
 Macro sequences execute faster than procedures because there is no CALL or RET
instruction to execute.
 The instructions of the macro are placed in your program by the assembler at the
point where they are invoked.
 Using macro avoids the overhead time involved in calling and returning
from a procedure.
Macros ...
 One drawback of macro is each time generating in-line code causes more memory
consumption
 The MACRO and ENDM directives delineate a macro sequence.
 The first statement of a macro is the MACRO instruction, which contains the name of the
macro and any parameters associated with it.
 An example is MOVE MACRO A,B
 which defines the macro name as MOVE. This new pseudo opcode uses two parameters: A
and B.
 The last statement of a macro is the ENDM instruction, which is placed on a line by itself.
 Never place a label in front of the ENDM statement. If a label appears before ENDM, the
macro will not assemble.
Syntax:
<Macro-name> MACRO [<Arg 1> <,Arg 2>…<,Arg n>]
..
ENDM
Macros ...
 Main parts Macro :
 MACRO header (MACRO)
 Text or Body
 Pseudo instructions marking the end of the instruction (e.g.:- ENDM)

MOV AX, P SWAP MACRO CHANGE MACRO P1, P2


MOV BX, Q MOV AX, P MOV AX, P1
MOV Q, AX MOV BX, Q MOV BX, P2
MOV P, BX MOV Q, AX MOV P2, AX
MOV P, BX MOV P1, BX
MOV AX, P ENDM ENDM
MOV BX, Q
CHANGE P, Q
MOV Q, AX SWAP
CHANGE R, S
MOV P, BX SWAP

You might also like