EEL 3801C EEL 3801 Part III Assembly Language Programming.

112
EEL 3801C EEL 3801 Part III Assembly Language Programming

Transcript of EEL 3801C EEL 3801 Part III Assembly Language Programming.

Page 1: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

EEL 3801

Part III Assembly Language

Programming

Page 2: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Assembly Language Programming

• The basic element of an assembly program is the statement.

• Two types of statements: • Instructions: executable statements that

actually do something. •Directive: provide information to assist the

assembler in producing executable code. For example, create storage for a variable and initialize it.

Page 3: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Assembly Programming (cont’d)

– Assembly language instructions equate one-to-one to machine-level instructions, except they use a mnemonic to assist the memory.

• Program control: Control the flow of the program and what instructions to execute next (jump, goto).

• Data transfer: Transfer data to a register or to main memory.

• Arithmetic: add, subtract, multiply, divide, etc.

Page 4: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Assembly Programming (cont’d)

• Logical: > < = etc.

• Input-output: read, print etc.

Page 5: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Statements• A statement is composed of a name, a

mnemonic, operands and an optional comment. Their general format is as follows:[name] [mnemonic] [operand(s)]

[;comments]

Page 6: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Names, labels– Name: Identifies a label for a statement

or for a variable or constant. • Can contain one or more of several characters

(see page 56 of new textbook).

• Only first 31 characters are recognized

• Case insensitive.

• First character may not be a digit.

Page 7: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Names, labels• The period “.” may only be used as the first

character.

• Cannot use reserved names.

• 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

Page 8: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Names, labels• 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

Page 9: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Mnemonics– Mnemonic: identifies an instruction or

directive. These were described above. • The mnemonics are standard keywords of the

assembly language for a particular processor. • You will become familiar with them as time

goes on this semester.

Page 10: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Operands– Operands are various pieces of

information that tells the CPU what to take the action on. Operands may be a register, a variable, a memory location or an immediate value.

10 immediate valuecount variable

AX register

[0200] memory location

– Comments: Any text can be written after the statement as long as it is preceded by the “;”.

Page 11: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Elements of Assembly Language for the 8086

Processor• Assembler Character Set These are used to form the names, mnemonics, operands, variables, constants, numbers etc. which are legal in 8086 assembly.

• Constant: A value that is either known or calculated at assembly time. May be a number or a string of characters. Cannot be changed at run time.

Page 12: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Elements of Assembly Language… (cont.)

• Variable: A storage location that is referenced by name. A directive must be executed identifying the variable name with the location in memory.

• Integers: Numeric digits with no decimal point, followed by the radix mentioned before (e.g., d, h, o, or b). Can be signed or unsigned.

Page 13: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Elements of Assembly Language… (cont.)

• Real numbers: floating point number made up of digits, a decimal point, an optional exponent, and an optional leading sign.(+ or -) digits.digits [exponential (+ or -)]

digits

Page 14: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Elements of Assembly Language… (cont.)

• Characters and strings: • A character is one byte long. • Can be mapped into the binary code equivalent

through the ASCII table, and vice-versa. • May be enclosed within single or double

quotation marks. • Length of string determined by number of

characters in string, each of which is 1 byte. For example:

Page 15: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Elements of Assembly Language… (cont.)

“a” – 1 byte long

‘b’ – 1 byte long

“stack overflow” – 14 bytes long

‘abc#?%%A’ – 8 bytes long

Page 16: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Example of Simple Assembly Program

• The following simple program will be demonstrated and explained:mov ax,5 ; move 5h into the AX register

add ax,10 ; add 10h to the AX register

add ax,20 ; add 20h to the AX register

mov sum,ax ; store value of AX in variable

; ending the program

mov ax, 4C00H

int 21

Page 17: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Example (cont.)• The result is that at the end of the

program, the variable sum, which exists somewhere in memory (declaration not shown), now accepts the value accumulated in AX, namely, 35h.

• Explain program.

Page 18: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

1:title Hello World Program (hello.asm)

2:

3:; this program displays “Hello, World”

4:

5:dosseg

6:.model small

7:.stack 100h

8:

9:.data

10: hello_message db ‘Hello, World!’,0dh,0ah,’$’

11:

Page 19: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

12: .code

13: main proc

14: mov ax,@data

15: mov ds,ax

16:

17: mov ah,9

18: mov dx,offset hello_message

19: int 21h

20:

21: mov ax,4C00h

22: int 21h

23: main endp

24: end main

Page 20: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

The program is explained as follows:•Line 1: Contains the title directive. All

characters located after the title directive are considered as comments, even though the ; symbol is not used.

•Line 3: Comment line with the ; symbol

•Line 5: The dosseg directive specifies a standard segment order for the code, data and stack segments. The code segment is where the program instructions are stored. The data segment is where the data (variables) are stored. The stack segment is where the stack is maintained.

Page 21: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

•Line 6: The .model directive indicates the memory architecture to be used. In this case, it uses the Microsoft small memory architecture. It indicates this by the word small after .model.

•Line 7: This directive sets aside 100h of memory for the stack. This is equivalent to 256 bytes of memory (162 = 256).

•Line 9: The .data directive marks the beginning of the data segment, where the variables are defined and memory allocated to them.

Page 22: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

•Line 10: The db directive stands for “define byte”, which tells the assembler to allocate a sequence of memory bytes to the data that follow. 0dh is a carriage return and 0ah is the linefeed symbol. The $ is the required terminator character. The number of memory bytes is determined by the data themselves. Hello_message is the name of the variable to be stored in memory, and the db allocates memory to it in the size defined by the data following it.

•Line 12: The directive .code is the indication of the beginning of the code segment. The next few lines represent instructions.

Page 23: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

•Line 13: The proc directive is used to declare the main procedure called “main”. Any name could have been used, but this is in keeping with the C/C++ programming requirement that the main procedure be called the main function. The first executable instruction following this directive is called the program entry point - the point at which the program begins to execute.

Page 24: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

•Line 14: The instruction mov is used to copy the contents of one address into the other one. The first operand is called the destination address, while the second one is called the source address. In this particular use, we tell the assembler to copy the address of the data segment (@data) into the AX register.

•Line 15: Copies the content of AX into DS, which is a register used to put the data segment, the default base location for variables.

Page 25: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

• Line 17: This instruction places the value 9 in the AH register. Remember that from the page.com program, this is the register used to store the name of the DOS subroutine to be called with the int 21 instruction.

• Line 18: This instruction places the address of the string to be identified in the DX register. Remember that this is the offset, where the default base segment is already identified in the DS register as the base segment for the data segment. Since the address of the variable hello_message begins at the beginning of the data segment, identified by DS, we only need to supply the offset for the variable.

Page 26: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

•Line 19: The instruction int 21, as we saw before, takes the name of the function from the DX register, which in this case is 9. DOS funtion 9, incidentally, sends the contents of DX register to the VRT output device. The DX register contains the address of the string to be sent.

•Line 21 and 22: These instructions represent the equivalent of an end or stop statement. This is different from that done for page.com because this will be an executable program (.exe), rather than a .com program. More on this later.

•Line 23: Indicates the end of the main procedure.

Page 27: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

•Line 24: The END directive – the last line to be assembled. The main next to it indicates the program entry point.

Page 28: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

– The program may seem overly complicated for such a simple program.

– But remember that assembly language corresponds one-to-one with machine language instructions.

– Note that it takes only 562 bytes of memory when assembled and compiled.

Page 29: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

More Complex Example (cont.)

– The same program written in a high level language will require several more machine level instructions to carry out the same thing.

– Written in Turbo C++, the executable program would take 8772 bytes of memory to store.

Page 30: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Specifics of ALP – Data Definition Directives

• A variable is a symbolic name for a location in memory. This is done because it is easy to remember variables, but not memory locations. It is like an aka, or a pseudonym.

Page 31: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Data Definition Directives

• Variables are identified by labels. A label shows the starting location of a variable’s memory location. A variable’s offset is the distance from the beginning of the data segment to the beginning of the variable.

Page 32: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Data Definition Directives (cont.)

• A label does not indicate the length of the memory that the variable takes up.

• If a string is being defined, the label offset is the address of the first byte of the string (the first element of the string). – The second element is the offset + 1 byte. – The third element is offset +2 bytes.

Page 33: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Data Definition Directives (cont.)

• The amount of memory to be allocated is determined by the directive itself.

DB

DW

DD

DF DP

DQ

DT

Define byte

Define word

Define doublewordDefine far

pointer

Define quadword

Define tenbytes

Page 34: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Byte• Allocates storage for one or more 8-bit

values (bytes). Has the following format:[name] DB initialvalue [,initialvalue]

• The name is the name of the variable. Notice that it is optional.

Page 35: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Byte (cont)• initialvalue can be one or more 8-bit

numeric values, a string constant, a constant expression or a question mark.

• If signed, it has a range of 127 to –128, If unsigned, it has a range of 0 – 255.

Page 36: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Byte - Examplechar db ‘A’ ; ASCII character

signed1 db -128 ; min signed value

signed2 db 127 ; max signed value

unsigned1 db 0 ; min unsigned value

unsigned2 db 255 ; max signed value

Page 37: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Byte (cont) • Multiple values: A sequence of 8-bit

numbers can be used as initialvalue. They are then grouped together under a common label, as in a list.

• The values must be separated by commas.

list db 10,20,30,40

Page 38: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Byte (cont) • The 10 would be stored at offset 0000; • 20 at offset 0001; • 30 at offset 0002; and • 40 at offset 0003, • where 0001 represents a single byte.

Page 39: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Byte (cont) • A variable’s value may be left

undefined. This can be done by placing a ‘?’ for each byte to be allocated (as in a list).

count db ?

Page 40: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Byte (cont) • A string may be assigned to a variable,

each of whose elements will be allocated a byte.

c_string db ‘This is a long string’

• The length of a string can be automatically determined by the assembler by the ‘$’ symbol.

• See page 65 of new book for details.

Page 41: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Word• Serves to allocate memory to one or

more variables that are 16 bits long. Has the following format:

[name] DW initialvalue [,initialvalue]

• The name is the name of the variable. Notice that it is optional.

• initialvalue can be one or more 16-bit numeric values, a string constant, a constant expression or a question mark.

Page 42: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Word (cont) • If signed, it has a range of 32,767 to –

32,768, • If unsigned, it has a range of 0 –

65,535.

Page 43: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Word (Example)var dw 1,2,3 ; defines 3 words

signed1 dw -32768 ; smallest signed value

signed2 dw 32767 ; largest signed value

unsigned1dw 0 ; smallest unsigned value

unsigned2dw 65535 ; largest signed value

var-bin dw 1111000011110000b

var-hex dw 4000h

var-mix dw 1000h,4096,’AB’,0

Page 44: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Reverse Storage Format• The assembler reverses the bytes in a

word when storing it in memory. – The lowest byte is placed in the lowest

address. – It is re-reversed when moved to a 16-bit

register.value dw 2AB6h

B6 2A

Page 45: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Define Doubleword – DD• Same as DB and DW, except the

memory allocated is now 4 bytes (2 words, 32 bits).

[name] DD initialvalue [,initialvalue]

• The name is the name of the variable. Notice that it is optional.

• initialvalue can be one or more 32-bit numeric values, either in dec., hex or bin. form, string const., a const. Expression, or ?

Page 46: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Multiple Values• A sequence of 32-bit numbers can be

used as initialvalue. • They are then grouped together under

a common label, as in a list. • The values must be separated by

commas.

Page 47: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Reverse Order Format• As in define word, the bytes in

doubleword are stored in reverse order as in DW.

• For example, var dd 12345678h

78 56 34 12

Page 48: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Type Checking

• When a variable is created, the assembler characterizes it according to its size (i.e., byte, word, doubleword, etc.).

• When a variable is later referenced, the assembler checks its size and only allows values to be stored that come from a register or other memory that matches in size.

• Mismatched movements of data not allowed.

Page 49: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Data Transfer Instructions – mov

• The instruction mov is called the data transfer instruction.

• A very important one in assembly - much programming involves moving data around.

• Operands are 8- or 16-bit on the 8086, 80186 and 80286.

• Operands on the 80386 and beyond, they can also be 32-bits long.

Page 50: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Data Transfer Instructions – mov (cont)

• The format is:mov destination,source

– The source and destination operands are self-explanatory. • The source can be an immediate value (a

constant), a register, or a memory location (variable). It is not changed by the operation.

• The destination can be a register or a memory location (variable).

Page 51: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Operands• Register Operands: Transfer involving

only registers. It is the fastest. • Source = any register• Destination = any register except CS and IP

• Immediate Operands• Immediate value can be moved to a register

(all but IP) or to memory. • Destination must be of same type as source.

Page 52: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Operands (cont.)• Direct operands

• A variable may be one of the two operands, but not both. It does not matter which is the variable.

Page 53: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Limitations on operands• There are some limitations on mov:

• CS or IP not destination operand• Moving immediate data to segment registers.• Moving from segment register to segment

register.• Source and destination operands of different

types. • Immediate value as destination (obviously!!)• Memory to memory moves

Page 54: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Sequential Memory Allocation• Memory for variables is allocated

sequentially by the assembler. • If we call DB several times, such as in:

var1 db 10

var2 db 15

var3 db 20

Page 55: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Sequential Memory Allocation (cont)

• var1 will be the first byte in the data segment of main memory.

• This segment may be identified by the base segment and the offset.

• var2 will occupy the next available memory location, or 1 byte away from the beginning of the data segment in memory.

Page 56: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Sequential Memory Allocation (cont)

• var 3 will be 2 bytes away from this starting point.

• This will be the case even if the memory locations are not labeled, such as in:

db 10

db 20

db 30

Page 57: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Offsets• Many times, memory will be allocated, but

not labeled.• This is typical of an array, when only the

entire array is labeled, not each cell. • The address of the array is the address of

the first element (position) of the array. • All subsequent cells are allocated by

adding an offset to the address of the head element.

Page 58: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Offsets• This is also true when a list of elements

is defined through DB, DW, or DD. – Example: an array or list of 8-bit numbers

whose memory location is called a-list. • To access the first element of a-list, we

reference the location in memory corresponding to a-list.

• To access any of the other elements of the array, we provide an offset to the address of array.

– The second element at array+1, the third at array+2, the fourth at array+3, etc.

Page 59: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Offsets• To move the value of the 5th element of

the array to register AL:mov al array+4

• The size of the two operands must match.– Otherwise, an error may result.

• Note that AL is used, not AX - 1 byte.

Page 60: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

PTR Operator• Used to clarify an operand’s type. NOT

a pointer. • It can be placed between the mov

command and the operands, as for example: Used for readability only. It does not change the size of the operand in any way.

mov word ptr count,10

mov byte ptr var2,5

Page 61: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

XCHG Instruction• Allows the direct exchange of values

between 2 registers or between a register and a memory location. – Very fast– Needs to obey size constraints– Used in sorting.

Page 62: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Stack Operations• Already discussed what a stack is.• Each position in the stack is 2 bytes

long – only 16-bit registers can be copied into the stack.

• The “bottom” of the stack is in high memory, and it grows downward.

• Typically, 256 bytes are allocated to the stack, enough for 128 entries.

Page 63: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Stack Operations (cont) • Identified by the SS register (stack

segment), which identifies the address of the base location of the stack segment.

• The stack pointer (SP register) indicates the address of the first element of the stack (top of the stack).

Page 64: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Push Operation

• Puts something (a 16-bit element) at the top position of the stack.

• Decrements stack pointer (it grows downward).

• Can put the contents of a register or of memory (a variable)

• In 80286 and later processors, it can also place an immediate value.

Page 65: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Push Operation• Has the following form:

push ax

push ar1

push 1000h

Page 66: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Pop Operation• The opposite of the push operation. • Removes the top element in the stack. • Copies value of top element in stack to

destination indicated in the statement. • Increments stack pointer.• Registers CS (code segment) and IP

(instruction pointer) cannot be used as operands.

Page 67: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Pop Operation• Has the following form:

pop ax

pop ar2

Page 68: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

PUSHF and POPF• Special instructions that move and

remove the contents of the Flags register onto and out of the stack.

• These are used to preserve the contents of these registers in case it is changed and the old values are to be reinstated.

• See page 56.

Page 69: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

PUSHA (80186+) and PUSHD (80386+)

• Pushes the contents of the registers AX, CX, DX, BX, original SP, BP, SI, and DI on the stack in this exact order.

• PUSHD does the same for 32-bit registers.

Page 70: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

POPA and POPD• Pops the same registers in the reverse

order.

Page 71: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Arithmetic Instructions• Form the heart of any program, at any

level of abstraction. • Integer arithmetic done in 8- or 16-bit

operands.

Page 72: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Arithmetic Instructions– Floating point arithmetic done in one of

three ways:

• 80x87 math coprocessor

• software routines that emulate the coprocessor

• software that converts floating point to integer, executes the instruction and then converts back to floating point. (not particularly useful in many cases).

– Section concentrates on integer arithmetic

Page 73: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

INC and DEC Instructions• Respectively increment and

decrement the operand by one.• Form is as follows:inc al ; incr. 8-bit register by 1inc bx ; incr. 16-bit register by 1inc mem ; incr. memory location by 1dec mem ; decr. memory location by 1dec bx ; decr. 16-bit register by 1dec al ; decr. 8-bit register by 1

Page 74: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

INC and DEC Instructions• Faster than ADD and SUB instructions• All status flags are affected except the

Carry Flag.

Page 75: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

ADD Instruction • Used to add two numbers together. • Format is as follows:

add destination,source• The high level language equivalent of: destination = destination + source

Page 76: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

ADD Instruction• Takes 2 operands, a source and a

destination.• Adds the contents of the operands.• Places the result in the destination.• Only one memory operand may be

used.• Source may be immediate value, but

not destination.

Page 77: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

ADD Instruction (cont.)• The contents of the source remain

unchanged.• A segment register may not be a

destination. • All status flags are affected.• Sizes of operands must match (same

size).

Page 78: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

SUB Instruction• Subtracts a source operand from a

destination operand and stores the result in the destination.

• Format is as follows:sub destination,source

• The high level language equivalent of: destination = destination + (-source)

Page 79: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

SUB Instruction (cont.)• Takes the two’s complement of source

and adds it to the destination.• Only one of the operands may be a

memory operand.• Size of operands must match (same

size).• Segment register may not be the

destination operand.

Page 80: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

SUB Instruction (cont.)• Only one of the operands may be a

memory operand.• Immediate values cannot be

destinations.

Page 81: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Effects of SUB and ADD on Flag Registers

• Why the flags? – If an operation such as ADD overflows (i.e.,

number to be put in the destination exceeds the size of the destination), then overflow.

– The flag may indicate that the value of the destination is meaningless.

– The ADD instruction, therefore, affects both the Carry and Overflow flags.

– Only 1 of these flags (if signed or if unsigned).

Page 82: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Registers (cont.)– Zero Flag: Set when the result of the

instructions inc, dec, add or sub = 0.– Sign Flag: Set when the result of the

instructions inc, dec, add or sub < 0– Carry Flag: Used with unsigned arithmetic

only, even though the processor updates it even if operation is signed. • For example:

Page 83: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Examplemov ax,00FFhadd al,1 ; AX = 0000h, CF=100FFh + 0001h = 0100h

– Since the operand of the add instruction is 8-bit (AL register), this is an 8-bit operation.

– Therefore, the operation looks more like

FFh + 01h = 100h

Page 84: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Example (cont.)– This is an overflow, so the Carry Flag is set

to indicate that the result was larger than the 8-bit destination could store.

– Could be fixed by using a 16-bit operation (e.g.,)

add ax,1 ; now it’s a 16-bit operation

; AX = 0100h CF = 0

Page 85: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Example (cont.)• If result of operation is too large for the

destination operand, the Carry flag is set. • Can also occur in a subtraction operation when

subtracting a larger operand from a smaller one (signed results are not permissible).

mov ax,5501h

sub al,2 ; AX = 55FFh CF=1• We subtracted 2 from 1, resulting in a negative

number in an unsigned operation.

Page 86: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Registers (cont.)• Overflow Flag: Set by the processor

regardless of the type of operation, but important only when operation is signed.– Signed results are useless when this flag is

set– Can result when using add or sub– But not inc or dec.

Page 87: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Registers - Example• If we add 126 + 2 = 128, in an 8-bit

operation, this will represent an overflow.

• This operation in binary numbers:01111111 + 00000010 = 10000000

• Equivalent to the two’s complement of –128, which is completely incorrect, therefore meaningless in signed integers.

• Therefore, OF = 1.

Page 88: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Registers - Example (cont.)

• In the case of subtraction, it gets a little more complicated. – If we subtract 2 from –128:– -128 – 2 = -130 which is an overflow, as

it does not fit in an 8-bit destination. – In binary, this looks like this:

10000000 (-128 in two’s complement) + 11111110 (-2 in twos’ comp)

= 1 01111110

Page 89: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Flag Registers - Example (cont.)

• The first 1, of course, is a ninth digit, which does not fit into the 8-bit register.

• Thus, the operation looks like it resulted in

01111110• which is –126. • So, the OF = 1.

Page 90: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Addressing Modes - the OFFSET

• Five (5) different addressing modes in the intel 8086 family of processors.

• Introduce an operator called offset, which places the address (actually, the offset) of a variable in a register.

• To move the address of a variable into a register, but do not know it directly, the OFFSET operator returns variable’s address

Page 91: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Addressing Modes - Example• The assembler always knows the

address of each variable (i.e., label). • For example,

mov ax,offset avariable

Page 92: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Direct Addressing Mode• Returns the contents of memory at the

offset of a variable. • The assembler keeps track of the offset

of every label (variable). • Thus, by simply referencing the label,

the contents of the memory location assigned to that label can be accessed.

Page 93: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Direct Addressing Mode (cont.)

• The effective address (EA) is the offset (i.e., distance) from the beginning of a segment.

• A displacement is either the absolute address, or the offset of a variable.

Page 94: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Direct Addressing Mode - Example

count db 20

mov al,count ; AL = 20 moved the ;contents of the label ;count into AL

• Can also be done by referencing the memory address within brackets,– for example, [200]

Page 95: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Indirect Addressing Mode• Placing the address of the label in a

base or index register can create a pointer to a label.

• Typically done to represent a complex data structure, such as an array or a structure.

• We can increment the pointer to point to elements of the data structure.

Page 96: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Indirect Addressing Mode - Example

• An array such as shown below.A B C D E F G H I J K L M N

• Let us define this array as a string in the following manner:astring db “ABCDEFGHIJKLMN”

Page 97: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Indirect Addressing Mode - Example

• To access the 6th element (F), set the value of a register to the first element of the array (string).

mov bx,offset astring ; moved the ;offset

• Then increment the memory address or offset, by 5, such as

add bx,5; add 5 bytes to address in BXmov dl,[bx] ; move content of BX into DL

Page 98: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Indirect Addressing Mode - Example

• If the BX, SI or DI registers are used, the effective (absolute) address is understood to be an offset from the DS register (by default).

• If BP register is used, it is understood to be an offset from the SS register.

• One can change the default by placing before the register the base register from which the offset is calculated.

Page 99: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Indirect Addressing Mode - Example

• For example: • To use the BP register, but calculate its

address from the DS register rather than from the SS:

mov dl,ds:[bp] ; looks in data segment DS

Page 100: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Based and Indexed Addressing Mode

• A register value and a displacement can be used as addresses to access the contents of memory.

• The displacement is either a number, or the offset of a label whose address (offset) is known by the assembler at compile time.

Page 101: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Based and Indexed Addressing Mode - Example

• For example, to access the number 8:

5 7 1 6 2 8 7 9 0

anarray db 5,7,1,6,2,8,7,9,0mov bx,5mov al,anarray[bx]ormov al,[anarray+bx]ormov al,[anarray+5]

Page 102: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Base-Indexed Addressing Mode

• Combining a base register and an index register forms the effective address.

• Useful for defining 2 dimensional arrays.

• Similar to the Base and indexed, except now instead of having a displacement that is either a constant or the address of a label, it is now the sum of the base and index registers.

Page 103: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Base-Indexed - Examplea2darray db 10,20,30,40,50

db 60, 70, 80, 90, A0

db B0, C0, D0, E0, F0• If we want to access the third element

of the second row, we set a base segment to the first element of the second row, and then the offset is the third column.

Page 104: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Base-Indexed - Example (cont.)

• For example, let’s say we want to get the ‘80’:

10 20 30 40 50 60 70 80 90 A0 B0 C0 D0 E0 F0

mov bx,offset a2darray

add bx,5 ; base=1st element of row 2

mov si,2 ;offset = 2

mov al,[bx+si] ; moves the ;value 80 into AL

Page 105: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Base-indexed with Displacement Addressing

Mode• The combination of the 2 addressing modes above.

• Combines the displacement of a label with that of a base and index segments.

• The same ‘80’ value in the above array can be accessed with the operand:

mov al, array[bx+si]

Page 106: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Program Structure

• Program divided into 3 primary segments:• code segment (pointed to by register CS)• data segment (pointed to by register DS)• stack segment (pointed to by register SS)

• Segments can range in size between 10h (16) bytes and 64K bytes.

• Instructions or data located on these segments is referenced through the offsets from the base segment (displacements).

Page 107: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Program Structure (cont.)• Each segment is identified by

the .stack, .code, and .data indicators.

• Note that the SP register points to the next memory location after the end of the stack segment. – This is because the stack grows from high

memory to low memory.

Page 108: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Program Structure (cont.)• The title directive identifies the

program title (up to 128 characters long).

• The dosseg directive tells the assembler to place the program segments in the standard order used by the high level languages.

• The .model directive identifies the type of memory model to be used.

Page 109: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Program Structure (cont.)• The memory models are based on the

idea that a 16-bit address register can only represent approximately 64K bytes of memory range.

• Thus, if we have a segment of memory that is less than or equal to 64K bytes, then we can easily access that location through a simple offset from the base segment.

Page 110: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Program Structure (cont.)• This means quicker addressing, since

only 1 instruction is necessary to load a 16-bit address.

• If greater than 64K, however, then we must change the base segment register as well, as the offset cannot reach beyond 64K bytes.

• This requires 2 machine instructions, one for the base segment, and one for the offset.

Page 111: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Program Structure (cont.) • The various models are the following:

– Tiny: Code + data <= 64K– Small: Code <= 64K; data <= 64K– Medium: Data <= 64K; Code any size– Compact: Code <= 64K; Data any size– Large: No restrictions on either one, but

arrays <= 64K– Huge: No restrictions on any of the

three

Page 112: EEL 3801C EEL 3801 Part III Assembly Language Programming.

EEL 3801C

Program Structure (cont.)• The Tiny model does not result in an

executable file, but rather, in a command file (.com).

• All others result in .exe files.• The linker produces a map file which

indicates the location of these segments.