COSC323 Module 7
COSC323 Module 7
COSC323 Module 7
PROGRAMMING IN
INTEL 8086
ASSEMBLY
Intel 8086 CPU Registers Cont’d
• The Intel 8086 CPU has 14 internal registers, each one of 16 bits (2bytes).
• The full name of the fourteen registers are:
AX Accumulator
BX Base register
CX Counting register
DX Data register
DS Data segment register
ES Extra segment register
SS Stack segment register
CS Code segment register
BP Base pointers register
SI Source index register
DI Destination index register
SP Stack pointer register
IP Next instruction pointer register
Flags register in the ALU
Intel 8086 CPU Registers Cont’d
AX (AH & AL) Accumulator Reg. Arithmetic operations (ADD, SUB, DIV & MUL)
BX (BH & BL) Base address Reg. Index register for MOVE, Memory, DIV and
MUL operations
CX (CH & CL) Count register For ASCII string operations
DX (DH & DL) Data register Port address for IN and OUT operations
Knowing the syntax in Intel 8086 assembly language and memory Access
Some of the following are important syntax to note when writing Intel assembly language:
Instructions or mnemonic codes can be written either in uppercase letter or lowercase letter. MOV mov Mov mOv
Common Intel 8086 Instructions
MOV Instruction
The MOV instruction takes two operands, representing the destination where data is to be placed and the source of that
data.
In 8086 assembly language, the source and destination cannot both be memory locations in the same instruction.
MOV copies a number in a register or in a memory location (a variable) i.e. it assigns a value to a register or variable.
The following illustrates the MOV instruction.
Copies the second operand (source) to the first operand (destination).
The source operand can be an immediate value, general-purpose register or memory location.
The destination register can be a general-purpose register, or memory location.
Both operands must be the same size, OR the source operand must smaller than the destination which can be a byte or
a word.
MOV Instruction Cont’d
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
The MOV instruction cannot be used to set the value of the CS and IP registers.
MOV Instruction Cont’d
Solution: A has ASCII code 65D (01000001B, 41H). See appendix A in the text book
Therefore, the following mov instruction can be used to carry out this task as:
mov bx, 65d
This could also be written as:
For example:
If COUNT is the label of a memory location the following are possible assembly-language instructions:
MOV AX, BX ; register: move contents of BX to AX
MOV AX, COUNT ; direct: move contents of the address labeled COUNT to AX
(where COUNT is a variable, e.g. COUNT = 10)
MOV CX, 0F0H ; immediate: load CX with the value 240
MOV BX, [0F0H] ; memory indirect: load BX with the value at address 240
MOV [BX], AL ; register indirect: move contents of AL to memory location in
BX
Using Arithmetic & Logic Instructions
Example:
mov ax, 5 ; load 5 into ax
add ax, 3 ; add 3 to the contents of ax, ax now contains 8
inc ax ; add 1 to ax, ax now contains 9 (Only Operand)
dec ax ; subtract 1 from ax, ax now contains 8 (only operand)
sub ax, 6 ; subtract 6 from ax, ax now contains 2
Note that the inc and dec instructions take only one operand. The mul (or imul for sign value)
and div (or idiv for sign value) also take only one operand.
SOME COMMON FUNCTIONS (Emu8086 Macro functions)
To make programming easier there are some common functions that can be included in the program.
To use any of the functions in emu8086.inc the following line must be included in the beginning of
the source file:
include 'emu8086.inc'
PUTC char - macro with 1 parameter; prints out an ASCII char at current cursor position.
PRINTN string - macro with 1 parameter; prints out a string. The same as PRINT but
automatically adds "carriage return" at the end of the string.
include emu8086.inc
ORG 100h
GOTOXY 5, 5
When compiler process the source code it searches the emu8086.inc file for declarations of
the macros and replaces the macro names with real code.
Emu8086 procedure functions
For example:
CALL PTHIS
db 'Hello World!', 0
- To use it declare: DEFINE_PTHIS before END directive.
Emu8086 procedure functions Cont’d
ORG 100h
LEA SI, msg1 ; ask for the number (LEA is Load Effective Address)
CALL print_string ;
CALL scan_num ; get number in CX.
CALL pthis
DB 13, 10, 'You have entered: ', 0
DEFINE_SCAN_NUM
DEFINE_PRINT_STRING
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS ; required for PRINT_NUM.
DEFINE_PTHIS
E. g.
Syntax for a variable declaration:
name DB value
name DW value
name - can be any letter or digit combination, though it should start with a letter
value - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or "?"
symbol for variables that are not initialized
Intel Variable Cont’d
ORG 100h
var1 DB 7
var2 DW 1234h
end
NOTE that:
DW can be used instead of DB if it's required to keep values larger than 255, or smaller than -128.
Intel Variable Cont’d
Both OFFSET and LEA can be used to get the offset address of the variable.
Note that:
LEA is more powerful because it also allows the programmer to get the address of indexed variables.
; data
msg1 DB "Enter your name: ", 0
newln DB 13, 10
DB "Hello, "
buffer DB 20 DUP (0) ; input buffer for get_string
bufSize = $-buffer ; calculates size of buffer
DEFINE_GET_STRING
DEFINE_PRINT_STRING
END ; directive to stop the compiler.
; demonstrate scan_num, print_num, pthis
;----------------------------------------
include 'emu8086.inc'
ORG 100h
; data
msg1 DB 'Enter the number: ', 0