Vous êtes sur la page 1sur 4

Review 2102440 Introduction to Microprocessors Lecture 4

Addressing modes Assembly programming A sample program Directives

Suree Pumrin, Ph.D. Pumrin,


1
2102440 Introduction to Microprocessors

Topics
Simplified segment definition
Directives

Shell of an Assembly Program (Simplified Segment Definition)


;THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM USING SIMPLIFIED SEGMENT DEFINITION .MODEL SMALL .STACK 64 .DATA ; ;place data definitions here ; .CODE MAIN PROC FAR ;this is the program entry point MOV AX,@DATA ;load the data segment address MOV DS,AX ;assign value to DS ; ;place code here ; MOV AH,4CH ;set up to INT 21H ;return to DOS MAIN ENDP END MAIN ;this is the program exit point

Control transfer instructions Instruction set of x86


Unsigned addition and subtraction

Executing assembly programs

2102440 Introduction to Microprocessors

2102440 Introduction to Microprocessors

Directives
PAGE and TITLE
PAGE [lines], [column] tell the printer how the list should be printed. Default setting PAGE (no numbers after it): 66 lines per page and max. 80 and characters PAGE 60,132 the range of number of lines = 10 t0 255 and range of columns = 60 to 132. TITLE put the name of the program and a brief description of the function of function the program.

Control Transfer Instructions


To transfer program control to a different location.
NEAR: control transfer within the current code segment intrasegment (within segment)
- A NEAR jump same. IP is updated; CS remains the

.STACK ; marks the beginning of the stack segment .DATA ; marks the beginning of the data segment .CODE; marks the beginning of the code segment .STACK 64 ; reserves 64 bytes of memory for the stack

FAR: control transfer outside the current code segment intersegment (between segment)
- A FAR jump
5

mov ax,@data ; data refers to the start of the data segment mov ds,ax
2102440 Introduction to Microprocessors

both IP and CS are updated.


6

2102440 Introduction to Microprocessors

8086 Conditional Jump (I)


Mnemonic JA/JNBE JAE/JNB JB/JNAE JBE/JNA JC JE/JZ JG/JNLE JGE/JNL JL/JNGE Condition Tested (CF = 0) and (ZF = 0) CF = 0 CF = 1 (CF or ZF) = 1 CF = 1 ZF = 1 ((SF xor OF) or ZF) = 0 (SF xor OF) = 0 (SF xor OF) = 1 Jump IF above/not below nor zero above or equal/not below below/not above nor equal below or equal/not above carry equal/zero greater/not less nor equal greater or equal/not less less/not greater nor equal
7

8086 Conditional Jump (II)


Mnemonic JLE/JNG JNC JNE/JNZ JNO JNP/JPO JNS JO JP/JPE JS Condition Tested ((SF xor OF) or ZF) = 1 CF = 0 ZF = 0 OF = 0 PF = 0 SF = 0 OF = 1 PF = 1 SF = 1 Jump IF less or equal/not greater not carry not equal/not zero not overflow not parity/parity odd not sign overflow parity/parity even sign
2102440 Introduction to Microprocessors

2102440 Introduction to Microprocessors

Short Jumps
All conditional jumps are short jumps. The address of the target must be within -128 to +127 bytes of the IP (256 possible addresses). - for the backward jump and + for the forward jump.

Unconditional jump
JMP label label To transfer unconditionally to the target location label.
SHORT JUMP JMP SHORT label (-128 to label 127 bytes) NEAR JUMP JMP label (within the current label code segment); the target address can be any of the addressing modes of direct, register, or register indirect. FAR JUMP JMP FAR PTR label (CS and IP label are replaced with new values)
2102440 Introduction to Microprocessors

2102440 Introduction to Microprocessors

10

CALL statements
CALL instruction is used to call a procedure. NEAR call within the current code segment. Only IP is saved on the stack. FAR call > outside the current code segment. Both CS and IP are saved on the stack.
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------MAIN PROC FAR ; This is the entry point for DOS MOV AX,@DATA MOV DS, AX CALL SUBR1 CALL SUBR2 MOV AH,4cH INT 21H MAIN ENDP ;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SUBR1 PROC ... ... RET SUBR1 ENDP ;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SUBR2 PROC ... ... RET SUBR2 ENDP ;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------END MAIN ; This is the exit point

Unsigned Addition and Subtraction


Addition of unsigned numbers
ADD destination, source ;
dest. operand = dest. operand + source operand dest. dest.

ADC destination, source;


Dest. Operand = dest. Operand + source operand + carry Dest. dest.

The destination operand can be a register or in memory. The source operand can be a register, in memory, or immediate. Memory-to-memory operations are not ALLOWED in Memory- to80x86.

2102440 Introduction to Microprocessors

11

2102440 Introduction to Microprocessors

12

Executing an Assembly Program


1.

Assembly Program Example 1


Example 1: Addition of individual byte and word data Write a program calculates the total sum of 5 bytes of data. Each byte represents the daily wages of a worker. This person does not make more than $255 (FFH) a day. The decimal data is as follows: 125, 235, 197, 91, 48.

Edit yourfile.asm using one of the follows:


notepad yourfile.asm wordpad yourfile.asm edit yourfile.asm

2.

Compile the source code to create an object module


tasm /z/zi yourfile.asm (The /z switch causes TASM to display the lines that generate compilation errors. The /zi switch enables /zi information needed by the debugger to be included in the .OBJ file.) file.)

3. 4.

Run the Linker to generate an .EXE file from the .OBJ file
tlink yourfile

Run the Program


Your final program (if there were no errors in the previous step) will step) have an .EXE ending. To just run it, type: yourfile

5.

If want to debug your program


td yourfile

2102440 Introduction to Microprocessors

13

2102440 Introduction to Microprocessors

14

Sample Program 1
;This program calculates the total sum of 5 bytes of data. Each byte represents the daily wages ; of a worker. This person does not make more than $255 (FFH) a day TITLE PROG3-1A (EXE) ADDING 5 BYTES PROG3PAGE 60,132 .MODEL SMALL .STACK 64 ;-------------.DATA COUNT EQU 05 DATA DB 125,235,197,91,48 ORG 0008H SUM DW ? ;-------------2102440 Introduction to Microprocessors

Sample Program 1 (cont.)


.CODE MAIN PROC MOV MOV MOV MOV MOV ADD JNC INC INC DEC JNZ MOV MOV INT ENDP END FAR AX,@DATA DS,AX CX,COUNT SI,OFFSET DATA AX,00 AL,[SI] OVER AH SI CX BACK SUM,AX AH,4CH 21H MAIN
16

BACK: OVER:

;CX is the loop counter ;SI is the data pointer ;AX will hold the sum ;add the next byte to AL ;If no carry, continue ;else accumulate carry in AH ;increment data pointer ;decrement loop counter ;if not finished, go add next byte ;store sum ;go back to DOS

MAIN

15

2102440 Introduction to Microprocessors

Assembly Program Example 2


Example 2: Addition of mutiword numbers Write a program to calculate the total sum of five words of data. Each data represents the yearly wages of a worker. This person does not make more than $65,555 (FFFFH) a year. The decimal data are 27345, 28521, 29533, 30105, and 32375.
2102440 Introduction to Microprocessors

Sample Program 2
;This program calculates the total sum of five words of data. Each data Each value represents the yearly ; wages of a worker. This person does not make more than $65,555 $65,555 (FFFFH) a year TITLE PROG3-1B (EXE) ADDING 5 WORDS PROG3PAGE 60,132 .MODEL SMALL .STACK 64 ;-------------.DATA COUNT EQU 05 DATA DW 27345,28521,29533,30105,32375 ORG 0010H SUM DW 2 DUP(?) ;-------------2102440 Introduction to Microprocessors

17

18

Sample Program 2 (cont.)


.CODE MAIN PROC MOV MOV MOV MOV MOV MOV ADD ADC INC INC DEC JNZ MOV MOV MOV INT ENDP END FAR AX,@DATA DS,AX CX,COUNT SI,OFFSET DATA AX,00 BX,AX AX,[SI] BX,0 SI SI CX BACK SUM,AX SUM+2,BX AH,4CH 21H MAIN ;CX is the loop counter ;SI is the data pointer ;AX will hold the sum ;BX will hold the carries ;add the next word to AX ;add carry to BX ;increment data pointer twice ;to point to next word ;decrement loop counter ;if not finished, continue adding ;store the sum ;store the carries ;go back to DOS

BACK:

MAIN

2102440 Introduction to Microprocessors

19

Vous aimerez peut-être aussi