26
University of Amsterdam Computer Systems – the instruction set Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

Embed Size (px)

DESCRIPTION

University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 3 Micro-instructions The ‘invoer’ is moved from memory to two registers (a,d), followed by operation subl int subtract(int invoer1, int invoer2) { return (invoer1 - invoer2); } _subtract: pushl%ebp movl%esp, %ebp movl12(%ebp), %edx movl8(%ebp), %eax subl%edx, %eax popl%ebp ret Register file ALU Gcc -S

Citation preview

Page 1: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 1

Computer Systems

The instruction set architecture

Page 2: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 2

Intel Processors

• A stable platform for nearly 20 years– 8086 (1978) 8 bits– 80186 (1980) 8 or 16 bits– 80286 (1982) 16 bits– 80386 (1985) 32 bits (33 MHz)– Pentium 4 (2001) 32 bits (3.2 GHz)

Page 3: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 3

Micro-instructions

• The ‘invoer’ is moved from memory to two registers (a,d), followed by operation subl

int subtract(int invoer1, int invoer2){ return (invoer1 - invoer2);}

_subtract:pushl %ebpmovl %esp, %ebpmovl 12(%ebp), %edxmovl 8(%ebp), %eaxsubl %edx, %eaxpopl %ebpret

Register file

ALU

Gcc

-S

Page 4: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 4

DisassemblersObjdump –d subtract.o │ gdb>disas subtract (page 205)

00000022 <_subtract>: 22: 55 push %ebp 23: 89 e5 mov %esp,%ebp 25: 8b 55 0c mov 0xc(%ebp),%edx 28: 8b 45 08 mov 0x8(%ebp),%eax 2b: 29 d0 sub %edx,%eax 2d: 5d pop %ebp 2e: c3 ret 2f: 90 nop

• The ‘l’ behind subl is omitted (refers to the standard 32-bits dataformat of IA32: ‘long word’ {int, float, pointer})

Page 5: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 5

Procedure call

• Stack is part of virtual memory• Every called procedure gets frame on the stack

Kernel virtual memory

Memory mapped region forshared libraries

Run-time heap(created at runtime by malloc)

User stack(created at runtime)

Unused0

Memoryinvisible touser code0xc0000000

0x08048000

0x40000000

Read/write data

Read-only code and dataLoaded from the hello executable file

printf() function

0xffffffff

Calling procedure

push parameter2 on stackPush parameter1 on stackCall subroutineClean parameters off stack

Page 6: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 6

Stack-based languages

• Languages that Support Recursion– e.g., C, Pascal, Java– Code must be “Reentrant”

• Multiple simultaneous instantiations of single procedure

– Need some place to store state of each instantiation

• Arguments• Local variables• Return pointer

• Stack Allocated in Frames– state for single procedure instantiation

Page 7: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 7

Stack Frames

• (gdb) info stack#0 subtract()#1 main()

• (gdb) up / down• (gdb) info frame

pointersArglistLocalsSaved registers Stack Pointer

(%esp)

Frame Pointer(%ebp)

Return Addr

SavedRegisters

+Local

Variables

ArgumentBuild

Old %ebp

Arguments

CallerFrame

Older Frames

Page 8: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 8

Call Chain ExampleCode Structureyoo(…){

••who();••

}

who(…){

• • •amI();• • •amI();• • •

}

amI(…){

••amI();••

}

yoo

who

amI

amI

amI

Call Chain

– Procedure amI recursive

amI

Page 9: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 9

StackPointer%esp

yoo

•••

%ebp

Stack Operation

yoo

Call Chainyoo(…){

••who();••

}

Page 10: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 10

StackPointer%esp

yoo

who

•••

FramePointer%ebp

Stack Operation

yoo

who

Call Chainwho(…){

• • •amI();• • •amI();• • •

}

Page 11: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 11

StackPointer%esp

yoo

who

amI

•••

FramePointer%ebp

Stack Operation

yoo

who

amI

Call ChainamI(…){

••amI();••

}

Page 12: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 12

StackPointer%esp

yoo

who

amI

•••

FramePointer%ebp

Stack Operation

yoo

who

amI

Call ChainamI(…){

••amI();••

}

amIamI

Page 13: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 13

StackPointer%esp

yoo

who

amI

•••

FramePointer%ebp

Stack Operation

yoo

who

amI

Call ChainamI(…){

••amI();••

}

amIamI

amI

amI

Page 14: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 14

StackPointer%esp

yoo

who

amI

•••

FramePointer%ebp

Stack Operation

yoo

who

amI

Call ChainamI(…){

••amI();••

}

amIamI

amI

Page 15: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 15

StackPointer%esp

yoo

who

amI

•••

FramePointer%ebp

Stack Operation

yoo

who

amI

Call ChainamI(…){

••amI();••

}

amI

amI

Page 16: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 16

StackPointer%esp

yoo

who

•••

FramePointer%ebp

Stack Operation

yoo

who

Call Chainwho(…){

• • •amI();• • •amI();• • •

} amI

amI

amI

Page 17: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 17

StackPointer%esp

yoo

who

amI

•••

FramePointer%ebp

Stack Operation

yoo

who

Call ChainamI(…){

••••

}amI

amI

amI

amI

Page 18: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 18

StackPointer%esp

yoo

who

•••

FramePointer%ebp

Stack Operation

yoo

who

Call Chainwho(…){

• • •amI();• • •amI();• • •

} amI

amI

amI

amI

Page 19: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 19

yoo(…){

••who();••

}

StackPointer%esp

yoo

•••

%ebp

Stack Operation

yoo

who

Call Chain

amI

amI

amI

amI

Page 20: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 20

Machine language

• Calling routinepush parm2push parm1call subroutine

• Calleepush %ebpmov %esp, %ebp…. body of subroutinepop %ebpret

Current frame

Caller’s frame

Saved %ebp

Saved registers,local variables,

andtemporaries

Argumentbuild area

Return addressArgument 1

Argument n•••

•••

•••

Frame pointer%ebp

Stack pointer%esp Stack “top”

Stack “bottom”

Increasingaddress

+4+8

+4+4n

–4

Earlier frames

Page 21: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 21

Subroutine Call and Return

– Push address of next instruction onto stack– Start executing instructions at Dest

– Pop value from stack– Use as address for next instruction

call Dest 8 0 Dest

ret 9 0

Page 22: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 22

IA32/Linux Register UsageInteger Registers– Two for the stack

%ebp, %esp– Three are callee-save

%ebx, %esi, %edisave on stack prior to using– Three are as caller-save

%eax, %edx, %ecxsave on stack prior to calling subroutine– Register %eax also stores

returned value

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

Caller-SaveTemporaries

Callee-SaveTemporaries

Special

Page 23: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 23

Summary

• The Stack Makes Recursion Work– Private storage for each instance of

procedure call• Instantiations don’t clobber each other• Addressing of locals + arguments can be

relative to stack positions– Can be managed by stack discipline

• Procedures return in inverse order of calls• Added as wrapper around ‘your’ code by

compiler

Page 24: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 24

Summary

• IA32 Procedures Combination of Instructions + Conventions– Call / Ret instructions– Register usage conventions

• Caller / Callee save• %ebp and %esp

– Stack frame organization conventions

Page 25: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 25

Conclusion• We understand know how

the simplest of subroutines is translated in micro-instructions

int subtract(int invoer1, int invoer2)

{ return (invoer1 - invoer2);}

_subtract:pushl %ebpmovl %esp, %ebpmovl 12(%ebp), %edxmovl 8(%ebp), %eaxsubl %edx, %eaxpopl %ebpret

Gcc

-S

Page 26: University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture

University of Amsterdam

Computer Systems – the instruction set architecture Arnoud Visser 26

Assignment

• Practice Problem 3.2 (page 142)1. Movl $0x4050, %eax Immediate → Register2. Movl %ebp, %esp Register → Register3. Movl (%edi,%ecx), %eax Memory → Register4. Movl $-17, (%esp) Immediate → Memory5. Movl %eax, -12(%ebp) Register → Memory