Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014."— Presentation transcript:

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

2 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)

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

4  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 ?!!

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

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

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

9 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 10 push 21ABH push 7FBD329AH pop EBX

11 Abed Asi - ESPL 11

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

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

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

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

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

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

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

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

20 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

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

22 Abed Asi - ESPL 22


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

Similar presentations


Ads by Google