CS 301 Fall 2002 Control Structures

Slides:



Advertisements
Similar presentations
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Advertisements

The University of Adelaide, School of Computer Science
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 10 S. Dandamudi.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Chapter 4 - Implementing Standard Program Structures in 8086 Assembly Language from Microprocessors and Interfacing by Douglas Hall.
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Strings, Procedures and Macros
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Lecture Set 4 Programming the 8051.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
1 Contents: 3.1 Instruction format and Addressing Modes 3.2 Instruction Introduction Chapter 3 Instruction system.
Chapter 12 Processor Structure and Function. Central Processing Unit CPU architecture, Register organization, Instruction formats and addressing modes(Intel.
Stack Operations Dr. Hadi AL Saadi.
Microprocessor Systems Design I
Microprocessor and Assembly Language
ECE 3430 – Intro to Microcomputer Systems
Microprocessor Systems Design I
Instruksi Set Prosesor 8088
Subroutines and the Stack
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Machine control instruction
Assembly IA-32.
Assembly Language Programming Part 2
Microcomputer Programming
Intel 8088 (8086) Microprocessor Structure
Microprocessor and Assembly Language
Chapter 3 Addressing Modes
Computer Science 210 Computer Organization
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Morgan Kaufmann Publishers Computer Organization and Assembly Language
CS 301 Fall 2002 Computer Organization
MIPS Instructions.
Subroutines and the Stack
The University of Adelaide, School of Computer Science
EECE.3170 Microprocessor Systems Design I
Practical Session 4.
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Chapter 6 - Procedures and Macros
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EECE.3170 Microprocessor Systems Design I
CNET 315 Microprocessor & Assembly Language
Subroutines and the Stack
Some Assembly (Part 2) set.html.
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

CS 301 Fall 2002 Control Structures Slide Set 5 9/21/2018

CS 301 Fall 2001 – Chapter 7 Slides by Prof. Hartman, following “IBM PC Assembly Language Programming” by Peter Abel 9/21/2018

The Stack Three main uses Saving return addresses for subroutines Passing data to subroutines Temporarily saving contents of registers so the program can use those registers for computation. 9/21/2018

The Stack 2 SS contains the address of the beginning of the stack, SP contains the size of the stack (and thus points to one address past the end of the stack) The stack begins storing data at the highest location in the segment and stores data downward through memory using PUSH and POP (and other) instructions. 9/21/2018

Stack Instructions PUSH, POP to and from general register, segment register, or memory PUSHA, POPA – save and restore contents of all general purpose registers (16 bytes) PUSHF, POPF – save and restore contents of the flags PUSHAD, POPAD – save and restore contents of all extended registers. (32 bytes) 9/21/2018

Instruction Execution and Addressing Processor steps in executing an instruction Fetch next instruction from memory and place it in instruction queue Decode the instruction: calculate addresses for memory references, deliver data to the ALU, increment IP Execute the instruction: perform the operation, store results in register or memory, set any required flags. 9/21/2018

Address types Short – Same segment, one byte offset, -128 to +127 Near – Same segment, two byte (80286 and earlier) or four byte (80386 and later) offset. Far – Different segment 9/21/2018

Branching Instructions JMP can jump to Short, Near, or Far addresses Jxx can jump to Short or Near (80386+) addresses LOOP can jump to Short addresses CALL can jump to Near or Far addresses 9/21/2018

Short Jumps If the label is before the jump (jumping back) NASM will automatically choose a Short jump if possible. If the label is after the jump (jumping forward) NASM will always use a Near jump, unless you specify jmp short label 9/21/2018

NASM labels Labels beginning with a period are “local” labels – they are associated with the most recent non-local label. 9/21/2018

Converting high-level control structures – if/else if ( condition ) { // body of then_block } else { // body of else_block In C is roughly equivalent to the following assembly code. Note the use of local labels. ; code to set flags based on condition jxx .else_block ; select xx to branch if false ; code for body of then_block jmp .endif .else_block: ; code for body of else_block .endif: 9/21/2018

Converting high-level control structures – while while ( condition ) { // body of loop } In C is roughly equivalent to the following assembly code. Note the use of local labels. .while: ; code to set flags based on condition jxx .endwhile ; select xx so that branches if false ; body of loop jmp .while .endwhile: 9/21/2018

Converting high-level control structures – do/while // body of loop } while ( condition ) In C is roughly equivalent to the following assembly code. Note the use of local labels. .do: ; code for body of loop ; code to set flags based on condition jxx .do ; select xx so branches if true 9/21/2018

Converting high-level control structures – for for(int i=0;i<10;++i) { // body of loop } In C is roughly equivalent to the following assembly code. Note the use of local labels. mov ecx, 10 .for: ; code for body of loop dec ecx jnz .for 9/21/2018

LOOP instruction LOOP label LOOPE/LOOPZ label LOOPNE/LOOPNZ label Decrements ecx (or cx in 16-bit mode) and branches to label unless ecx is then zero. LOOPE/LOOPZ label Adds condition that ZF=1. LOOPNE/LOOPNZ label Adds condition that ZF=0. 9/21/2018

Converting high-level control structures – for for(int i=0;i<10;++i) { // body of loop } In C is roughly equivalent to the following assembly code. Note the use of local labels. mov ecx, 10 .for: ; code for body of loop loop .for ; 9/21/2018

CALL and RET CALL proc_name RET [n] Pushes IP, sets IP to offset of proc_name (and clears processor’s prefetch instruction queue) RET [n] Pops IP (and clears processor’s prefetch instruction queue) Possibly “pops” n arguments from the stack 9/21/2018

Passing parameters Can pass parameters by reference (address) or value. Can pass parameters in registers or on stack. Examples using registers: regpassing.asm 9/21/2018

Passing parameters on the stack 1 Push parameters on the stack before the CALL instruction Procedure doesn’t pop them off, it accesses them directly on the stack: Avoids having to pop off return address then put it back on Allows using the parameter multiple times Need to use indirect addressing Examples using stack: stackpassing.asm 9/21/2018

Indirect Addressing Can add registers and/or constants and/or a location and get at what is located in the result MOV eax,[data] MOV eax,[ebx] MOV eax,[data+ebx] MOV eax,[ebx+2] MOV eax,[ebx*8+esp+4] 9/21/2018