Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.

Slides:



Advertisements
Similar presentations
Hello World!. PC / MS-DOS code segment para assume cs:code,ds:code org 0100h start: mov dx,offset message ;point to message mov ah,09h ;func# to printstring.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
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.
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.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 10 S. Dandamudi.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Introduction to Assembly Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
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.
Introduction to Assembly Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
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.
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.
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.
Functions/Methods in Assembly
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
Practical Session 5 Computer Architecture and Assembly Language.
Compiler Construction Code Generation Activation Records
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.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Computer Architecture and Assembly Language
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.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
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.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
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.
Practical Session 5.
Stack Operations Dr. Hadi AL Saadi.
Reading Condition Codes (Cont.)
Assembly language.
Data Transfers, Addressing, and Arithmetic
Computer Architecture and Assembly Language
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Computer Architecture and Assembly Language
Machine-Level Programming 4 Procedures
Stack and Subroutines Module M17.1 Section 11.2.
Practical Session 4.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
X86 Assembly Review.
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015

; Data section begins section.data var1 dd 40 var2 dd 20 var3 dd 30 section.text global _start _start: mov ecx, [var1] cmp ecx, [var2] jg check_third_var mov ecx, [var2] check_third_var: cmp ecx, [var3] jg _exit mov ecx, [var3] _exit: mov ebx, ecx mov eax, 1 int 80h Abed Asi - ESPL 2

section.text global _start ;must be declared for linker (ld) section.data msg db 'Hello world!',0xa ;our dear string len equ $ - msg ;length of our dear string _start: ;tell linker entry point mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel Abed Asi - ESPL 3

 Functions and the Stack  Pentium Implementation of the stack  Uses of the stack  Calling Procedures Abed Asi - ESPL 4

 A stack is a last-in-first-out (LIFO) data structure  The top-of-the-stack (TOS) is indicated by ESP register  The key characteristics:  Only words (16-bit) or doublewords (32-bit) are saved on the stack  The stack grows toward lower memory address (downward)  TOS always points to the last inserted data item  TOS points to the lower byte of the last inserted word Abed Asi - ESPL 5

push source pop destination  The operands can be a 16-bit or 32-bit general purpose registers, or a word or a doubleword in memory Abed Asi - ESPL 6

7

8 push 21ABH push 7FBD329AH pop EBX

Abed Asi - ESPL 9

 The stack is used for three main purposes Abed Asi - ESPL 10  Temporary Storage of Data  Transfer of Control  Parameter Passing

Abed Asi - ESPL 11

 The Pentium provides call and ret instructions  After the call instruction, the EIP points to the next instruction to be executed  The processor pushes the content of the EIP onto the stack call proc-name Abed Asi - ESPL 12 ESP = ESP – 4 ESP = EIP EIP = EIP + d High Low

 The ret instruction is used to transfer control from the called procedure to the calling procedure ret  Note: integral return value of procedures are stored in EAX 13 Abed Asi - ESPL High Low EIP = ESP ESP = ESP + 4

 It is more complicated than that used in high-level languages  The calling procedure first places all the parameters need by the called procedure in the stack Abed Asi - ESPL 14 For example, consider passing two 32-bit parameters to a SUM procedure pushnumber1 pushnumber2 call sum

 So, how do we retrieve the parameters now ?  Since the stack is a sequence of memory location ESP+4 points to number2, and ESP+8 to number1  For instance, to read number2 we can invoke: Abed Asi - ESPL 15 movEBX, [ESP+4] Are we done ? What type of problems we would encounter?

 The stack pointer is updated by the push and pop instructions  the relative offset changes  A better alternative is to use the EBP register Abed Asi - ESPL 16 movEBP, ESP mov AX, [EBP+4] Done? push EBP movEBP, ESP mov AX, [EBP+4]  Since every procedure uses the EBP register, it should be preserved

Abed Asi - ESPL 17 push number1 push number2 call sum sum: push EBP mov EBP, ESP mov ESP, EBP pop EBP ret

Abed Asi - ESPL 18 func: push EBP movEBP, ESP sub ESP, 8...

section.DATA string db “ESPL”,0 section.CODE mov EAX, string ;EAX = string[0] pointer push EAX inc EAX push EAX ;EAX = string[1] pointer call swap swap: push EBP mov EBP, ESP push EBX;save EBX – procedure uses EBX mov EBX, [EBP+12]; EBX = first character pointer xchg AL, [EBX]; swap between operands mov EBX, [EBP+8]; EBX = second character pointer xchg AL, [EBX] mov EBX, [EBP+12]; EBX = first character pointer xchg AL, [EBX] pop EBX mov ESP, EBP pop EBP ret Abed Asi - ESPL 19