Flow of Assembly Language

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Topic:: 

Topic: Flow of Assembly Language and procedures

GROUP MEMBERS: 

GROUP MEMBERS MAIRA ALVI 2K9-CSE-169 RAMSHA TARIQ 2K9-CSE-129 ZAIRA ASHRAF 2K9-CSE-130 FATIMA MASOOD 2K9-CSE-153

Contents: 

Contents Program Flow Control jumps Loops Procedures macro 3

Control Flow:: 

Control Flow: Almost all programming languages have the ability to change the order in which statements are evaluated ,and assembly is no exception. The IP register contains the address of next instructions to be executed . To change the flow of control, the programmer must be able to modify the value of IP. This is where control flow functions come in.

Modify instruction pointer:: 

Modify instruction pointer: Mov ip , label Jmp label ;Wrong ;right

IP can be modified by (indirectly): 

IP can be modified by (indirectly) Jumps Loops Call ( procedure,macro ) Interrupts

Jumps: 

Jumps Unconditional jump Jmp label Conditional jumps JA,JB,JZ,JE,JNZ,JNE,JP,JNP etc.

Unconditional Jumps: 

Unconditional Jumps There are three types of the JMP instruction: Short Jump Near Jump Far Jump Short and near jumps are intrasegment jumps and far jump is intersegment jump.

PowerPoint Presentation: 

Unlike JMP instruction that does an unconditional jump, there are instructions that do a conditional jumps (jump only when some conditions are in act) Conditional jumps are always short jumps . These instructions are divided in three groups: First group just tests single flag Second group compares numbers as signed Third group compares numbers as unsigned

PowerPoint Presentation: 

The general syntax for conditional jumps: <Conditional jump instruction> <label> Example: JC label1 If the condition under the test is true, a branch to the label occurs. If the condition is false, the next sequential step in the program executes.

Jump Instructions That Test Single Flag: 

Jump Instructions That Test Single Flag Instruction Description Condition Opposite JZ, JE Jump if Zero (Equal) Z = 1 JNZ, JNE JC, JB, JNAE Jump if Carry (Below, Not Above Equal) C = 1 JNC, JNB, JAE JS Jump if Sign S = 1 JNS JO Jump if Overflow O = 1 JNO JPE, JP Jump if Parity Even P = 1 JPO, JNP JNZ, JNE Jump if Not Zero (Not Equal) Z = 0 JZ, JE JNC, JNB, JAE Jump if Not Carry (Not Below, Above Equal) C = 0 JC, JB, JNAE JNS Jump if Not Sign S = 0 JS JNO Jump if Not Overflow O = 0 JO JPO, JNP Jum p if Parity Odd (No Parity) P = 0 JPE, JP * Different names are used to make programs easier to understand. 11

Jump Instructions for Signed Numbers: 

Jump Instructions for Signed Numbers Instruction Description Condition Opposite JE, JZ Jump if Equal (=) Jump if Zero Z = 1 JNE, JNZ JNE, JNZ Jump if Not Equal (≠) Jump if Not Zero Z = 0 JE, JZ JG, JNLE Jump if Greater (>) Jump if Not Less or Equal (not <=) Z = 0 and S = O JNG, JLE JL, JNGE Jump if Less (<) Jump if Not Greater or Equal S ≠ O JNL, JGE JGE, JNL Jump if Greater or Equal (>=) Jump if Not Less S = O JNGE, JL JLE, JNG Jump if Less or Equal (<=) Jump if Not Greater Z = 1 or S ≠ O JNLE, JG 12

Jump Instructions for Unsigned Numbers: 

Jump Instructions for Unsigned Numbers Instruction Description Condition Opposite JE, JZ Jump if Equal (=) Jump if Zero Z = 1 JNE, JNZ JNE, JNZ Jump if Not Equal (≠) Jump if Not Zero Z = 0 JE, JZ JA, JNBE Jump if Above (>) Jump if Not Below or Equal C = 0 and Z = O JNA, JBE JBE, JNA Jump if Below or Equal (<=) Jump if Not Above C = 1 or Z = 1 JNBE, JA JB, JNAE, JC Jump if Below (<) Jump if Not Above or Equal Jump if Carry C = 1 JNB, JAE, JNC JAE, JNB, JNC Jump if Above or Equal (>=) Jump if Not Below Jump if Not Carry C = 0 JB, JNAE, JC 13

CALL : 

CALL Transfers the flow of the program to the procedure . 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.

PowerPoint Presentation: 

Why save the IP on the stack ? the instruction pointer always points to the next instruction in the program For the CALL instruction, the contents of IP are pushed onto the stack. program control passes to the instruction following the CALL after a procedure ends

RET : 

RET R emoves a pushed address from the top of stack. automatically selects the proper return instruction

Procedures: 

Procedures Procedure is a part of code that can be called from your program in order to make some specific task. Procedures make program more structural and easier to understand. Generally procedure returns to the same point from where it was called. 30

Procedure Syntax: 

Procedure Syntax name PROC ; here goes the code ; of the procedure ... RET name ENDP name - is the procedure name the same name should be in the top and the bottom, this is used to check correct closing of procedures. 31

Procedure Example: 

Procedure Example SUMS PROC ADD AX, BX ADD AX, CX ADD AX, DX RET SUMS ENDP 32

Some Notes on the Syntax: 

Some Notes on the Syntax Probably, you already know that RET instruction is used to return to operating system. The same instruction is used to return from procedure (actually operating system sees your program as a special procedure). PROC and ENDP are compiler directives, so they are not assembled into any real machine code. Compiler just remembers the address of procedure. 33

Calling a Procedure: 

Calling a Procedure CALL instruction is used to call a procedure. Example: ORG 100H CALL m1 MOV AX, 2 RET ; return to operating system. m1 PROC MOV BX, 5 RET ; return to caller. m1 ENDP END 34

CALL and RET: 

CALL and RET CALL saves a return address on the stack. RET removes the return address from the stack and places it into IP . Don’t forget to put a RET at the end of a procedure! 43

Example: 

Example ORG 100h MOV AL, 1 MOV BL, 2 CALL m2 RET ; return to operating system. m2 PROC MUL BL ; AX = AL * BL. RET ; return to caller. m2 ENDP END 44

About the Example: 

About the Example The procedure takes its parameters from AL and BL ; multiplies them and stores the result into AX 45

Macros: 

Macros Macros are just like procedures, but not really Macros look like procedures, but they exist only until your code is compiled After compilation all macros are replaced with real instructions If you declared a macro and never used it in your code, compiler will simply ignore it. 46

Macro SYNTAX: 

Macro SYNTAX name MACRO [parameters,...] <instructions> ENDM 47

PowerPoint Presentation: 

Unlike procedures, macros should be defined above the code that uses it. 48 MyMacro MACRO p1, p2, p3 MOV AX, p1 MOV BX, p2 MOV CX, p3 ENDM ORG 100h MyMacro 1, 2, 3 MyMacro 4, 5, DX RET The code on the left is expanded into: MOV AX, 00001h MOV BX, 00002h MOV CX, 00003h MOV AX, 00004h MOV BX, 00005h MOV CX, DX

Macros and Procedures: 

Macros and Procedures When you want to use a procedure you should use CALL instruction, for example: CALL MyProc When you want to use a macro, you can just type its name. For example: MyMacro You should use stack or any general purpose registers to pass parameters to procedure. To pass parameters to macro, you can just type them after the macro name. For example: MyMacro 1, 2, 3 57

Macros and Procedures: 

Macros and Procedures Macros end with ENDM directive and procedures end with ENDP directive. Procedures are located at some specific address in memory but macros are expanded directly in program's code. 58

Loops: 

Loops Instruction Operation and Jump Condition Opposite Direction LOOP D ecrease CX, jump to label if CX not zero DEC CX and JCXZ LOOPE Decrease CX, jump to label if CX not zero and equal (Z = 1) LOOPNE LOOPNE D ecrease CX, jump to label if CX not zero and not equal (Z = 0) LOOPE LOOPNZ D ecrease CX, jump to label if CX not zero and Z = 0 LOOPZ LOOPZ D ecrease CX, jump to label if CX not zero and Z = 1 LOOPNZ JCXZ Jump to label if CX is zero OR CX, CX and JNZ 59

Algorithm: 

Algorithm CX = CX - 1 if CX <> 0 then jump else no jump, continue

Interrupts: 

Interrupts An interrupt is a way for the process to ”interrupt” the current instruction code path and switch to a different path. Interrupts come into two varieties: 1.Software interrupts 2.Hardware interrupts

Hardware interrupt: 

Hardware interrupt Hardware devices generates hardware interrupts. They are used to signal events happening at the hardware level. Program generate software interrupts. They are a signal to hand off control to another program.When a program is called by an interrupt, the calling program is put on hold , and the called program takes over. The instruction pointer is transferred to the called program, and execution continues from within the called program. When the called program is complete, it can return back to the calling program (using an interrupt return instruction).

Software Interrupts: 

Software Interrupts Software interrupts are provided by the operating system to enable application to tap into functions, within the operating system and in some cases even in the underlying BIOS operating system. For example When running the debugger that it performs the interrupt instruction but then immediately returns back to the normal program.

Conclusion: 

Conclusion we concluded that to change the flow of a program we need to modify instruction pointer which can be modified, but indirectly. We also come to know, after the comparison of procedures and macros that which one is faster and would be preferred. As Code in a macro is repeated every time a macro is called, but there is just one copy of code for procedure. Macros often execute more rapidly than procedure call since there is no overhead for passing parameters or for call or ret instructions, but there is usually at the cost of more bytes of object code.

Conclusion (continue): 

Conclusion (continue) As now there is no concern with memory due to the presence of huge memories, but efficiency matters a lot so, macros are more preferable than procedures as they are used in our many applications.

PowerPoint Presentation: 

Questions ?

THANKYOU FOR YOUR ATTENTION!: 

THANKYOU FOR YOUR ATTENTION!