Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

3 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

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

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

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

8 8 push 21ABH push 7FBD329AH pop EBX

9 Abed Asi - ESPL 9

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

11 Abed Asi - ESPL 11

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

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

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

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

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

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

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

19 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


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

Similar presentations


Ads by Google