Microprocessor and Assembly Language

Slides:



Advertisements
Similar presentations
Micro-Computer Applications: Jumping and Loop Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Advertisements

Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label.
Target Processor Directives , When using.386, the program can only run on 386 and above processors.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Flow Control Instructions
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Faculty of Engineering, Electrical Department,
CE302 MICROPROCESSORS Levent EREN Izmir University of Economics.
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Strings, Procedures and Macros
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
Review of Assembly language. Recalling main concepts.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Internal Programming Architecture or Model
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Addressing Modes Instruction – Op-code – Operand Addressing mode indicates a way of locating data or operands. – Any instruction may belong to one or more.
Assembly language programming
Microprocessor Systems Design I
Instruction set Architecture
Format of Assembly language
Data Transfers, Addressing, and Arithmetic
Microprocessor T. Y. B. Sc..
Homework Reading Labs PAL, pp
COURSE OUTCOMES OF MICROPROCESSOR AND PROGRAMMING
Additional Assembly Programming Concepts
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly IA-32.
Assembly Language Programming Part 2
Microprocessor and Assembly Language
(The Stack and Procedures)
Assembly Lang. – Intel 8086 Addressing modes – 1
Chapter 3 Addressing Modes
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
UNIT: 2 INSTRUCTION SET OF 8086.
CS 301 Fall 2002 Control Structures
X86’s instruction sets.
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
(Array and Addressing Modes)
Chapter 3: Addressing Modes
(The Stack and Procedures)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
(Array and Addressing Modes)
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EECE.3170 Microprocessor Systems Design I
Assembly Language for Intel 8086 Jump Condition
(The Stack and Procedures)
Chapter 7 –Program Logic and Control
By Nasser Halasa Assembly Language.
The JUMP GROUP Unconditional Jump (JMP).
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
(Array and Addressing Modes)
Procedures and Macros.
Presentation transcript:

Microprocessor and Assembly Language Program Control Instructions

Program Control Instructions The program control instructions direct the flow of a program and allow the flow to change Allows programmer to skip program sections and branch to any part of memory for the next instruction Type of program control instructions Unconditional transfers Conditional transfers Subroutine call and return

Unconditional Jump (JMP) JMP instruction unconditionally transfers control to another point in the program Types of unconditional jump instructions: short jump: allows jumps or branches to memory locations within +127 and –128 bytes from the address following the jump near jump: allows a branch or jump within ±32K bytes (or anywhere in the current code segment) far jump: allows a jump to any memory location within the real memory system The short and near jumps are often called intrasegment jumps Far jumps are called intersegment jumps

JMP Format Shot, near and far jump instructions generally use the same syntax, it is: jmp target The assembler differentiates them by their operands: jmp disp8 ;direct intrasegment(short), 8 bit displacement jmp disp16 ;direct intrasegment(near), 16 bit displacement jmp adrs32 ;direct intersegment(far), 32 bit segmented address jmp mem16 ;indirect intrasegment (near), 16 bit memory operand. jmp reg16 ;register indirect intrasegment(near). jmp mem32 ;indirect intersegment(far), 32 bit memory operand. The first form consists of an opcode and a single byte displacement. The CPU sign extends this displacement to 16 bits and adds it to the IP register The second form: The CPU simply adds the two byte displacement to the IP register. In third form: the address following the opcode is the absolute memory address of the target instruction. This instruction loads cs:ip with a 32 bit immediate value. The direct jumps are normally specify the target address using a statement label. A statement label is usually an identifier followed by a colon The assembler automatically computes the distance from the jump instruction to the statement label

Short JMP Example 1: XOR BX,BX START: MOV AX,1 ADD AX,BX JMP NEXT; or JMP short NEXT <skipped memory locations> NEXT: MOV BX,AX JMP START Example 2:

Near JMP A near jump that adds the displacement (0002H) to the contents of IP

Far JMP A far jump instruction replaces the contents of both CS and IP with 4 bytes following the opcode FAR PTR directive indicates a far jump instruction ; JMP FAR PTR UP Define far (global) label: label UP is defined as a far label by the EXTRN UP:FAR directive Or using UP:: …….. Ex. 1 : EXTRN UP:FAR NEXT: MOV BX,AX JMP UP Ex.2: ;<skipped memory locations> UP:: MOV BX,AX

Indirect Jump The address of the jump is in the register specified by the jump instruction , or memory location Unlike displacement associated with the near jump, register contents or memory location contents are transferred directly into the instruction pointer Indirect JMP using Register: JMP AX, for example, copies the contents of the AX register into the IP. allows a jump to any location within the current code segment Ex.: TABLE: DW ONE ; jump table DW TWO MOV SI,OFFSET TABLE ;address TABLE MOV AX,[SI] ;get ONE, TWO JMP AX ;jump to ONE, TWO ONE: MOV DL,’1’ ;get ASCII 1 JMP Start TWO: MOV DL,’2’ ;get ASCII 2

Indirect Jumps Using an Index Jump instruction may also use the [ ] form of addressing to directly access the jump table. The jump table can contain offset addresses for near indirect jumps, or segment and offset addresses for far indirect jumps. The assembler assumes that the jump is near unless the FAR PTR directive indicates a far jump instruction Ex.: TABLE: DW ONE ; jump table DW TWO MOV SI,1 JMP TABLE[SI] ;jump to ONE ONE: MOV DL,’1’ ;get ASCII 1 JMP Start TWO: MOV DL,’2’ ;get ASCII 2

Conditional Jumps Conditional jump instructions test flag bits: sign (S), zero (Z), carry (C) parity (P), overflow (0) To compare numbers there two sets of compare instruction: compare signed numbers compare unsigned numbers you will probably execute a conditional jump after a cmp instruction If the condition under test is true, a branch to the label associated with the jump instruction occurs. if false, next sequential step in program executes for example, a JC will jump if the carry bit is set The conditional jump instructions only test flags, they do not affect any of the 80x86 flags. Most conditional jump instructions are straightforward as they often test one flag bit. although some test more than one Allows a conditional jump to any location within the current code segment

Conditional Jumps The conditional jumps that test one flag:

Instructions for Unsigned Comparisons Terms above and below refer to unsigned numbers

Instructions for signed Comparisons Terms greater than and less than refer to signed numbers

The JCXZ/JECXZ Instructions The conditional jump instructions all test flag bits except for JCXZ (jump if CX = 0) JECXZ (jump if ECX = 0) Instead of testing flag bits, JCXZ directly tests the contents of the CX register without affecting the flag bits, and JECXZ tests the contents of the ECX register. For the JCXZ instruction, if CX = 0, a jump occurs, and if CX != 0, no jump occurs. Likewise for the JECXZ instruction, if ECX = 0, a jump occurs; if ECX != 0, no jump occurs. jcxz Target becomes test cx, cx ;Sets the zero flag if cx=0 je Target

Loop The LOOP instruction is a combination of a decrement CX and the JNZ conditional jump LOOP instruction decrements either CX or ECX, depending upon the instruction mode, if CX != 0 (or ECX!=0), it jumps to the address indicated by the label. If CX (ECX) becomes 0, the next sequential instruction executes Ex.: .Data LIST DB 100 DUP(0) .CODE .STARTUP MOV CX, 100 MOV BL, 1 L1: MOV BH,6 ADD BH, LIST [BL] MOV LIST[BL], BH INC BL LOOP L1 .EXIT END

Procedures A procedure is a group of instructions that usually performs one task. subroutine, method, or function is an important part of any system’s architecture A procedure is a reusable section of the software stored in memory once, used as often as necessary. saves memory space and makes it easier to develop software

Procedures Only disadvantage of procedure is time it takes the computer to link to, and return from it. CALL links to the procedure; the RET (return) instruction returns from the procedure CALL pushes the address of the instruction following the CALL (return address) on the stack. the stack stores the return address when a procedure is called during a program RET instruction removes an address from the stack so the program returns to the instruction following the CALL With the assembler, there are specific rules for storing procedures: A procedure begins with the PROC directive and ends with the ENDP directive. each directive appears with the procedure name PROC is followed by the type of procedure: NEAR (Local) or FAR (Global)

Procedure Example SUMS PROC NEAR SUMS ENDP SUMS1 PROC FAR SUMS1 ENDP ADD AX,BX ADD AX,CX ADD AX,DX RET SUMS ENDP SUMS1 PROC FAR SUMS1 ENDP MOV BX,1000H MOV CX,1000H MOV DX,1000H CALL SUMS ADD AX,1 .

CALL Instruction The CALL instruction transfers the flow of the program to the procedure The CALL instruction differs from the jump instruction because a CALL saves a return address on the stack The return address returns control to the instruction that immediately follows the CALL in a program when a RET instruction executes Near Call: identical to the form of the near jump instruction it first pushes the offset address of the next instruction (IP or EIP register)onto the stack . WHY? It then adds the displacement to the IP to transfer control to the procedure

Near CALL The effect of a near CALL on the stack and the instruction pointer

Far CALL Far CALL places the contents of both IP and CS on the stack before jumping to the address indicated by target. The far CALL instruction is like a far jump because it can call a procedure stored in any memory location in the system.

Far CALL The effect of a far CALL instruction

CALLs with Register Operands Like jump instructions, call instructions also may contain a register operand An example is the CALL BX instruction, which pushes the contents of IP onto the stack It then jumps to the offset address, located in register BX, in the current code segment This type of CALL always uses a 16-bit offset address, stored in any 16-bit register except the segment registers Ex.: .MODEL TINY ;select tiny model .CODE ;start code segment .STARTUP MOV BX,OFFSET DISP ;load BX with offset DISP MOV DL,’O’ ;display O CALL BX MOV DL,’K’ ;display K .EXIT ;Procedure that displays the ASCII character in DL DISP PROC NEAR MOV AH,2 ;select function 2 INT 21H ;execute DOS function 2 RET DISP END END

RET Instruction Removes a 16-bit number (near return) from the stack placing it in IP, or removes a 32-bit number (far return) and places it in IP & CS