מבנה מחשב תרגול 4 מבנה התוכנית. 2 introduction when we wrote: ‘int n =...

16
בבבב בבבב בבבבב4 בבבב בבבבבבב

Upload: jayson-flowers

Post on 18-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

מבנה מחשב

4תרגול מבנה התוכנית

Page 2: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

2

Introduction

When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’.

In Assembly, we’ll do it ourselves. Our program is comprised of 3 parts:

Data SegmentText (=Code) SegmentStack Segment

Page 3: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

3

The Data Segment

.data - start of data part of the code. We can give each data item a label, for

future reference, by writing:

my_label: Later the assembler will exchange the

labels with actual memory addresses. Every line must start with a ‘tab’.

Page 4: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

4

The Data Segment

We must specify the type of each data item, e.g. byte, word, double. For instance:counter: .word 15

In memory, it will look like: 00001111 00000000

The compiler will translate ’15’ to binary. Remember “word” = 16 bit

counter counter+1

Page 5: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

5

The Data Segment

Example:.data

vec: .word 12089, -89, 130

avi: .word 72

So ‘vec’ is actually an array of words. Each item should be read as word, else it would have a different meaning.

Page 6: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

6

The Data Segment

Another Example:.data

alice: .byte 3, 5 ,10, -7bob: .byte 9, 20

In the memory it will be stored sequentially:00000011 00000101 00001010 11111001 00001001 00010100

alice alice+1 alice+2 alice+3 bob bob+1

Page 7: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

7

The Data Segment

Each variable type can store a signed or an unsigned value.

Therefore, byte, for example can store a value in the range: -128 – 255.

For simply allocating memory space we can use:

.space 10*4

for example (10*4 clearer syntax for 10 variables of size 4 - rather than 40).

Page 8: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

8

The Data Segment

.section .rodata

means that from this point until .data or .text this section contains only read-only data.

Const, Global, permanent string data.

Page 9: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

9

The Text Segment

.text – start of the text part of the code.

for instance:

.text

addl $20, %eax

Page 10: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

10

The Stack Segment

We’ll use a stack, rather different then the “common” one.

We can read and write data anywhere on the stack.

But, for each procedure / function, we’ll add space in the stack in a LIFO manner.

The Stack Pointer (%esp) is a register pointing to the head of the stack.

Page 11: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

11

The Stack Segment

For example:

.text

addl $-64, %esp

… (the procedure itself)

addl $64, %esp

Page 12: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

12

The Stack Segment

Why did we add -64 (and not 64)?

Data Segment

Text Segment

Reserved for OS

Initial %esp

PC

New %esp

Stack Segment

0x00000000

0x00400000

0x10000000

0x7fffffff

New %ebp

Page 13: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

13

The Stack Segment

The Frame Pointer (%ebp) is used to store the contents of the stack pointer at the beginning of the procedure.

So if we’ll make an error managing the SP, we can recover its value, as it was when the procedure started.

Page 14: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

14

Registers

* Must be saved (pushed) in the stack before changing them, and restored when the program\function ends.

** Do not count on them !!! ,Don’t put any valuable information in them if you are going to call a function from outside your program (like: printf ,scanf etc..)

UsesizeRegister Name

Load to the lower 8 bits of these registers**8b%al, %cl, %dl

Load to bits 8-15 in these registers**8b%ah, %ch, %dh

Load the lower 8 bits and bits 8-15 of this register*8b%bl, %bh

Load to the lower 16 bits of these registers**16b%ax, %cx, %dx

Load to the lower 16 bits of this register*16b%bx

Caller save registers = Temporary registers**32b%ecx, %edx

Return value**32b%eax

Callee save registers = Save registers*32b%ebx, %esi, %edi

Contains the stack pointer 32b%esp

Contains the frame pointer32b%ebp

Page 15: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

15

Program Structure .data

l: … #all global data.….section .rodata

k: … #all RO data such as string formats for printf..text #the beginning of the code

.globl main #defining the label “main” as the starting point..type main, @function #defining “main” as function.

main:pushl %ebp #saving the old frame pointer.movl %esp, %ebp #creating the new frame pointer.… #saving callee-save registers if needed

… #The program code

… #restoring callee-save registers if neededmovl %ebp, %esp #restoring the old stack pointer.popl %ebp #restoring the old frame pointer.ret #returning to the function that called us.

Page 16: מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’

16

Stack Frame Structure

The stack is used for: passing arguments storing return information saving registers local storage