Cs 301 fall 2002 control structures
Download
1 / 20

CS 301 Fall 2002 Control Structures - PowerPoint PPT Presentation


  • 102 Views
  • Uploaded on

CS 301 Fall 2002 Control Structures. Slide Set 5. CS 301 Fall 2001 – Chapter 7. Slides by Prof. Hartman, following “IBM PC Assembly Language Programming” by Peter Abel. The Stack. Three main uses Saving return addresses for subroutines Passing data to subroutines

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CS 301 Fall 2002 Control Structures' - camilla-doyle


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Cs 301 fall 2002 control structures

CS 301 Fall 2002Control Structures

Slide Set 5


Cs 301 fall 2001 chapter 7

CS 301 Fall 2001 – Chapter 7

Slides by Prof. Hartman, following “IBM PC Assembly Language Programming” by Peter Abel


The stack
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.


The stack 2
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.


Stack instructions
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)


Instruction execution and addressing
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.


Address types
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


Branching instructions
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


Short jumps
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


Nasm labels
NASM labels

  • Labels beginning with a period are “local” labels – they are associated with the most recent non-local label.


Converting high level control structures if else
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:


Converting high level control structures while
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:


Converting high level control structures do while
Converting high-level control structures – do/while

do {

// 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


Converting high level control structures for
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 ecxjnz .for


Loop instruction
LOOP instruction

  • LOOP 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.


Converting high level control structures for1
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 ;


Call and ret
CALL and RET

  • CALL proc_name

    • 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


Passing parameters
Passing parameters

  • Can pass parameters by reference (address) or value.

  • Can pass parameters in registers or on stack.

  • Examples using registers: regpassing.asm


Passing parameters on the stack 1
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


Indirect addressing
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]


ad