21
Lectures schedule 2 When ? Topic Lectu re 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, 2013 Introduction to Assembly 3 November 17, 2013 November 10, 2013 Functions and System Calls (Assembly) 4 Midterm A ( December 4, 2013) December 8, 2013 Unix Processes 5 December 15, 2013 Programs Execution 6 December 22, 2013 Introduction to script languages (Python) 7

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

Embed Size (px)

Citation preview

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

2

Lectures schedule

When ? Topic Lecture

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, 2013 Introduction to Assembly 3

November 17, 2013November 10, 2013

Functions and System Calls (Assembly) 4

Midterm A ( December 4, 2013)

December 8, 2013 Unix Processes 5

December 15, 2013 Programs Execution 6

December 22, 2013 Introduction to script languages (Python) 7

January 5, 2014 Web programming 8

Midterm B (January 15, 2014)

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

3

From last lecture - Registers

Abed Asi - ESPL

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

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

4

From last lecture - Conditional Jump

Jump if the specified condition is satisfied

j<cond> label ;identifies the condition

The condition being tested is the result of the last arithmetic or logic operation

read_char:mov DL,0. . . (code for reading a character into AL). . .cmp AL,0DH ;compares the character to CRje CR_received ; if equal, jump to CR_receivedinc CL ;otherwise, increment CL andjmp read_char ; go back to read another char.

CR_received:mov DL, AL

Abed Asi - ESPL

but, the CMP doesn’t save the result, so what really happens ?!!

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

5

From last lecture - Loops

mov CL,50repeat1:<loop body>dec CLjnz repeat1. . .. . .

Abed Asi - ESPL

mov ECX,50repeat1:

<loop body>loop repeat1. . . . . .

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

6

Today

Functions and the Stack Pentium Implementation of the stack Uses of the stack Calling Functions

Abed Asi - ESPL

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

7

Pentium Implementation of the Stack

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

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

8

Pentium Implementation of the Stack

Abed Asi - ESPL

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

9

Basic Instructions

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

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

10

Basic Instructions - Examples

Abed Asi - ESPL

push 21ABHpush 7FBD329AH

pop EBX

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

11

Stack Operations

Abed Asi - ESPL

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

12

Use of the Stack

The stack is used for three main purposes

Abed Asi - ESPL

Temporary Storage of Data

Transfer of Control

Parameter Passing

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

13

Temporary Storage of Data

Abed Asi - ESPL

value1 and value2 are in memory We want to exchange their values mov doesn’t work, why ?

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

14

Transfer of Control

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

<return address >

ESP = ESP – 4ESP = EIPEIP = EIP + d

High

Low

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

15

Transfer of Control

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

Abed Asi - ESPL

High

Low

<return address>

EIP = ESPESP = ESP + 4

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

16

Parameter Passing

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

For example, consider passing two 16-bit parameters to a SUM procedure

push number1push number2call sum

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

17

Parameter Passing

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

mov EBX, [ESP+4]

Are we done ? What type of problems we

would encounter?

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

18

Parameter Passing 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

mov EBP, ESPmov AX, [EBP+4]

Done?

push EBPmov EBP, ESPmov AX, [EBP+4]

Since every procedure uses the EBP register, it should be preserved

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

19

Parameter Passing

Abed Asi - ESPL

push number1push number2call sum

sum:push EBPmov EBP, ESP<SUM CODE>mov ESP, EBPpop EBPret

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

20

Example section .DATA

string db “ESPL”,0

section .CODEmov EAX, string ;EAX = string[0] pointer push EAXinc EAXpush EAX ;EAX = string[1] pointercall swap

swap:push EBPmov EBP, ESPpush EBX ;save EBX – procedure uses EBXmov EBX, [EBP+12] ; EBX = first character pointerxchg AL, [EBX] ; swap between operandsmov EBX, [EBP+8] ; EBX = second character pointerxchg AL, [EBX]mov EBX, [EBP+12] ; EBX = first character pointer

xchg AL, [EBX]pop EBXmov ESP, EBPpop EBPret

Abed Asi - ESPL

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

21

Local Variables

Abed Asi - ESPL

func:push EBPmov EBP, ESPsub ESP, 8...

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

22

Interrupts

Abed Asi - ESPL