Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.

Slides:



Advertisements
Similar presentations
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
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.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
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.
Accessing parameters from the stack and calling functions.
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.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CS2422 Assembly Language & System Programming September 26, 2006.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Computer Architecture and Assembly Language
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 1.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Assembly language.
Format of Assembly language
C function call conventions and the stack
Computer Architecture and Assembly Language
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Practical Session 4.
Multi-modules programming
X86 Assembly Review.
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Assembly 07

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 1

Boxes within Boxes Procedures help manage code complexity Procedures make code more: Readable Maintainable Reusable 2 What does this code do again?

Boxes within Boxes Unlike high-level languages, assembly does not “ship” with built-in procedures (e.g., printf in C++) You have to write your own procedures Read from file Write to file Etc. 3

Boxes within Boxes Book uses example of “getting up in the morning” Shut off clock radio Climb out of bed Let dogs out Eat breakfast Brush your teeth Shower 4

Boxes within Boxes Each task can be divided into smaller tasks: E.g., Brushing your teeth: Pick up toothpaste Unscrew cap Place cap on sink counter … Same idea with procedures Divide tasks into subtasks 5

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 6

Procedure Definition Must begin with a label Must have at least one ret (return) E.g., my_print:; label for “my_print” proc ; some instruction ret; return 7

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 8

call Mnemonic call ->used to invoke the procedure usage: call ; 9

ret Mnemonic ret->returns from procedure usage: ret;takes no operands ; returns to instruction after call 10

call / ret Mnemonics call puts esp onto stack esp has address of next instruction (after call) ret pops esp from stack Execution resumes at the instruction after call 11

12 my_print: ; ret; ; call my_print; ; … stack esp 1. instruction executes

13 my_print: ; ret; ; call my_print; ; … stack esp 2. call to my_print made

14 my_print: ; ret; ; call my_print; ; … stack esp 3. esp+1 value pushed to stack address of ;

15 my_print: ; ret; ; call my_print; ; … stack esp 4. flow of execution goes to my_print address of ;

16 my_print: ; ret; ; call my_print; ; … stack 5. my_print executes address of ; esp

17 my_print: ; ret; ; call my_print; ; … stack 6. ret (return) reached in my_print address of ; esp

18 my_print: ; ret; ; call my_print; ; … stack 7. Address popped off stack 8. esp set to address address of ; esp

19 my_print: ; ret; ; call my_print; ; … stack 9. Flow of execution continues at instruction after call esp

20 my_print: ; ret; ; call my_print; ; … stack 10. Flow of execution continues… esp

21 stack Every time you make a call, the stack grows by 32 bits. Why?? return address

22 stack Every time you return from a procedure (with ret), the stack shrinks by 32 bits. return address

msg: db “Hello There!!!”,10;in.data msgLen: equ $-msg; in.data call my_print;in.text my_print:; in.text mov eax, 4; make sys_write call mov ebx, 1; write to stdout mov ecx, msg; write contents of msg mov edx, msgLen; number of bytes to write int 0x80; make system call ret; return

call / ret Mnemonics UNIX>./a.out Hello World!!! UNIX> 24 msg: db “Hello There!!!”,10 msgLen: equ $-msg call my_print my_print: mov eax, 4 mov ebx, 1 mov ecx, msg mov edx, msgLen int 0x80 ret msg: db “Hello There!!!”,10 msgLen: equ $-msg call my_print my_print: mov eax, 4 mov ebx, 1 mov ecx, msg mov edx, msgLen int 0x80 ret

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 25

Saving Register Values What is the result of add eax, ebx? mov eax, 42; mov ebx, 58; call my_print; add eax, ebx; 26

Saving Register Values mov eax, 42; mov ebx, 58; call my_print; add eax, ebx; 27 eax ebx

Saving Register Values mov eax, 42; mov ebx, 58; call my_print; add eax, ebx; eax ebx

Saving Register Values mov eax, 42; mov ebx, 58; call my_print; add eax, ebx; eax ebx my_print sets eax to “4” for sys_write my_print sets ebx to “1” for stdout

Saving Register Values mov eax, 42; mov ebx, 58; call my_print; add eax, ebx; eax ebx were we expecting “5” or “100”??

Saving Register Values Often important to save registers before calling a procedure Guards against bugs that are extremely hard to track down 31 spooky

Saving Register Values Use the stack to save registers Push registers onto stack before procedure call Pop registers off of stack after procedure returns Be mindful of order! Last In First Out 32

Saving Register Values If you “own” the procedure code, you can push / pop registers within the procedure Push registers at beginning of procedure Pop registers at end of procedure 33

Saving Register Values mov eax, 42; mov ebx, 58; pushad; call my_print; popad; add eax, ebx; 34 eax ebx

Saving Register Values mov eax, 42; mov ebx, 58; pushad; call my_print; popad; add eax, ebx; eax ebx

Saving Register Values mov eax, 42; mov ebx, 58; pushad; call my_print; popad; add eax, ebx; eax ebx all 32-bit GP registers pushed to stack

Saving Register Values mov eax, 42; mov ebx, 58; pushad; call my_print; popad; add eax, ebx; eax ebx

Saving Register Values mov eax, 42; mov ebx, 58; pushad; call my_print; popad; add eax, ebx; eax ebx pop all 32-bit registers from stack back to registers

Saving Register Values mov eax, 42; mov ebx, 58; pushad; call my_print; popad; add eax, ebx; eax ebx

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 40

Passing Arguments How do you pass arguments to assembly procedures? … and no, this is not a setup for a hilarious joke… Use registers! Caller puts arguments into pre-ordained registers E.g., eax is argument 1, ebx is argument 2, etc. Important to clearly comment your procedures!! Especially expectations for arguments E.g., integer in eax, pointer in ebx, etc. 41

Passing Arguments Example: procedure to add two numbers, print result to stdout Numbers will be arguments to procedure Simplified version: only prints results between 0 and 9… Printing results > 10 may be part of your homework… 42

SIZE: equ 10; in.data output: resb SIZE ; in.bss ; below in.text mov eax, 2; argument 1 in eax mov ebx, 3; argument 2 in ebx call my_add; invoke procedure to add/print my_add:; procedure to add / print result add eax, ebx; add arguments add eax, ‘0’; convert to ASCII number mov [output], eax;; put result in output buffer mov byte [output+1], 10 ; add carriage return call my_print; write output buffer to stdout ret; return to caller

Procedure Arguments 44 UNIX>./a.out 5 UNIX> msg: SIZE: equ 10 output: resb SIZE mov eax, 2 mov ebx, 3 call my_add my_add: add eax, ebx add eax, ‘0’ mov [output], eax mov byte [output+1], 10 call my_print ret msg: SIZE: equ 10 output: resb SIZE mov eax, 2 mov ebx, 3 call my_add my_add: add eax, ebx add eax, ‘0’ mov [output], eax mov byte [output+1], 10 call my_print ret

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 45

Return Value(s) How do you return value(s) from assembly procedures? Register(s), of course!! Example: convert character from lowercase to uppercase 46

mov al, ‘a’; argument 1 in al call convert; convert ‘a’ to ‘A’ ; cl now has converted covert:; procedure to convert mov cl, al; copy argument 1 sub cl, 32; make return value uppercase ; (‘A’ is 32 less than ‘a’) ret; return to caller

mov al, ‘a’; call convert; covert: mov cl, al sub cl, 32 ret al cl

mov al, ‘a’; call convert; covert: mov cl, al sub cl, 32 ret 96 = 0x61 = ‘a’ al cl

mov al, ‘a’; call convert; covert: mov cl, al sub cl, 32 ret 96 = 0x61 = ‘a’ al cl

mov al, ‘a’; call convert; covert: mov cl, al sub cl, 32 ret 96 = 0x61 = ‘a’ al cl

mov al, ‘a’; call convert; covert: mov cl, al sub cl, 32 ret 96 = 0x61 = ‘a’ 65 = 0x41 = ‘A’ al cl

mov al, ‘a’; call convert; covert: mov cl, al sub cl, 32 ret 96 = 0x61 = ‘a’ 65 = 0x41 = ‘A’ al cl now cl has return value ‘A’

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 54

Global vs. Local Data Global: can be accessed anywhere in code Local: can only be accessed “locally” (e.g., within procedure) In x86 assembly, all declared data items are global Can declare data items in.text (!!!) Appear local, but actually global 55

Examples of Local Data Using the stack to temporarily store data in procedures Data pushed to stack (during procedure) only visible to that procedure Data items declared in external code libraries Only visible to external code (But there is a mechanism to make global) 56

Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data Accidental Recursion 57

Accidental Recursion “Uncommon but inevitable bug” Watch out for incorrect base case Pay attention to instruction => flags Will eventually run out of stack space Each call pushes 32-bits onto stack (Why?) Eventually causes a Segmentation Fault (Why?) 58

Accidental Recursion Example “Not so accidental” recursion call recur;; 1st instruction in.text ; after _start recur: call my_print; call procedure to print “LINE” call recur; make recursive call ret; never reached.. 59

Accidental Recursion Example UNIX>./a.out LINE... (on and on..) Segmentation Fault UNIX> 60 call recur; recur: call my_print call recur ret call recur; recur: call my_print call recur ret stdout in BLUE stderr in GREEN

Accidental Recursion Example UNIX>./a.out > output.txt Segmentation Fault UNIX> head –n 2 output.txt LINE UNIX> wc –l output.txt output.txt 61

Accidental Recursion Example UNIX>./a.out > output.txt Segmentation Fault UNIX> head –n 2 output.txt LINE UNIX> wc –l output.txt output.txt 62 > : redirect stdout to a file named “output.txt” stderr ignored

Accidental Recursion Example UNIX>./a.out > output.txt Segmentation Fault UNIX> head –n 2 output.txt LINE UNIX> wc –l output.txt output.txt 63 head –n 2 output.txt : output top 2 lines of file output.txt output.txt contains “LINE” printed once per line

Accidental Recursion Example UNIX>./a.out > output.txt Segmentation Fault UNIX> head –n 2 output.txt LINE UNIX> wc –l output.txt output.txt 64 wc –l output.txt : count number of lines in file output.txt output.txt contains 2,094,543 lines. recur called 2+ million times!!