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

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

COMP 2003: Assembly Language and Digital Logic
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
Lecture 6 Machine Code: How the CPU is programmed.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
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.
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.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
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,
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
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.
Types of Registers (8086 Microprocessor Based)
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.
1 ICS 51 Introductory Computer Organization Fall 2009.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
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.
Functions/Methods in Assembly
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.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Computer Architecture and Assembly Language
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
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.
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.
Stack Operations Dr. Hadi AL Saadi.
Reading Condition Codes (Cont.)
Assembly language.
Instruction set Architecture
Format of Assembly language
Data Transfers, Addressing, and Arithmetic
Aaron Miller David Cohen Spring 2011
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
Symbolic Instruction and Addressing
CS 301 Fall 2002 Control Structures
BIC 10503: COMPUTER ARCHITECTURE
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Practical Session 4.
Symbolic Instruction and Addressing
Morgan Kaufmann Publishers Computer Organization and Assembly Language
University of Gujrat Department of Computer Science
Computer Architecture CST 250
X86 Assembly Review.
Chapter 6 –Symbolic Instruction and Addressing
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:

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

2 When ?TopicLecture October 20, 2013 Introduction to C Programming in Unix Environment - I 1 October 27, 2013 Introduction to C Programming in Unix Environment - II 2 November 3, 2013Introduction to Assembly3 November 17, 2013 November 10, 2013 Functions and System Calls (Assembly)4 Midterm A ( December 4, 2013) December 8, 2013Unix Processes5 December 15, 2013Programs Execution6 December 22, 2013Introduction to script languages (Python)7 January 5, 2014Web programming8 Midterm B (January 15, 2014)

Abed Asi - ESPL 3  Pentium has bit and 6 16-bit registers  Registers are grouped into:  General registers  Control registers  Segment registers  General registers  Data registers  Pointer registers  Index registers

 Jump if the specified condition is satisfied j label ;identifies the condition  The condition being tested is the result of the last arithmetic or logic operation read_char: movDL,0... (code for reading a character into AL)... cmpAL,0DH ;compares the character to CR jeCR_received ; if equal, jump to CR_received incCL ;otherwise, increment CL and jmpread_char ; go back to read another char. CR_received: movDL, AL Abed Asi - ESPL 4 but, the CMP doesn’t save the result, so what really happens ?!!

mov CL,50 repeat1: dec CL jnz repeat1... Abed Asi - ESPL 5 mov ECX,50 repeat1: loop repeat1...

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

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

8

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 9

10 push 21ABH push 7FBD329AH pop EBX

Abed Asi - ESPL 11

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

Abed Asi - ESPL 13  value1 and value2 are in memory  We want to exchange their values  mov doesn’t work, why ?

 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 (of the calling function) onto the stack call proc-name Abed Asi - ESPL 14 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 15 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 16 For example, consider passing two 16-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+6 to number1  For instance, to read number2 we can invoke: Abed Asi - ESPL 17 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 18 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 19 push number1 push number2 call sum sum: push EBP mov EBP, ESP mov ESP, EBP pop EBP ret

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 20

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

Abed Asi - ESPL 22