University*of*Washington*

Size: px
Start display at page:

Download "University*of*Washington*"

Transcription

1 Roadmap* C:* car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly* language:* Machine* code:* Computer* system:* get_mpg: pushq movq... popq %rbp %rsp, %rbp %rbp Java:* Car c = new Car(); c.setmiles(100); c.setgals(17); float mpg = c.getmpg(); OS:* Memory*&*data* Integers*&*floats* Machine*code*&*C* x86*assembly* Procedures*&*stacks* Arrays*&*structs* Memory*&*caches* Processes* Virtual*memory* Memory*allocaJon* Java*vs.*C* Procedures*and*Call*Stacks* How*do*I*pass*arguments*to*a*procedure?* How*do*I*get*a*urn*value*from*a*procedure?* Where*do*I*put*local*variables?* When*a*funcJon*urns,*how*does*it*know*where*to*urn?* To*answer*these*quesJons,*we*need*a*call$stack* * 1* 2* Memory*Layout* Memory*Layout* segmentajon*faults?* 2 N -1 Stack local variables; procedure context writable; not executable Stack Managed automatically (by compiler) Dynamic Data (Heap) variables allocated with new or malloc writable; not executable Dynamic Data (Heap) Managed by programmer Static Data static variables (including global variables (C)) writable; not executable Static Data Initialized when process starts Literals literals (e.g., example ) read-only; not executable Literals Initialized when process starts 0 Instructions read-only; executable Instructions Initialized when process starts 3* 4*

2 IA32*Call* Region*of*memory*managed** with*a*stack* discipline * Grows*toward*lower*addresses* Customarily*shown* upside[down * Bo^om * Increasing& Addresses& IA32*Call*Stack:*Push* pushl Src* Bo^om * Increasing& Addresses& Register**contains** lowest*stack*address* =*address*of* top *element* Pointer:* Stack&Grows& Down& Pointer:* Stack&Grows& Down& Top * Top * 5* 6* IA32*Call*Stack:*Push* IA32*Call*Stack:*Pop* Bo^om * Bo^om * pushl Src* Fetch&value&from&Src& Decrement&*by&4&&(why(4?)( Store&value&at&address&& given&by& Increasing& Addresses& popl Dest* Increasing& Addresses& Pointer:* [4* Stack&Grows& Down& Pointer:* Stack&Grows& Down& Top * Top * 7* 8*

3 IA32*Call*Stack:*Pop* IA32*Call*Stack:*Pop* Bo^om * Bo^om * popl Dest* Load&value&from&address&* Write&value&to&Dest( Increment&*by&4& Increasing& Addresses& popl Dest* Load&value&from&address&* Write&value&to&Dest( Increment&*by&4& Increasing& Addresses& Pointer:* +4* Stack&Grows& Down& Pointer:* +4* Stack&Grows& Down& Top * Top * Those*bits*are*sJll*there;* we re*just*not*using*them.* 9* 10* Procedure*Call*Overview* Procedure*Call*Overview* Caller* procedures* Caller* **** * <set*up*args>* call* <clean*up*args>* <find*urn*val>* **** * Callee*must*know*where*to*find*args* Callee* <create*local*vars>* *** * <set*up*urn*val>* <destroy*local*vars>* urn* **** * <save$regs>* <set*up*args>* call* <clean*up*args>* <restore$regs>$ <find*urn*val>* **** * Callee* <save$regs>$ <create*local*vars>* *** * <set*up*urn*val>* <destroy*local*vars>* <restore$regs>$ urn* Callee*must*know*where*to*find* urn*address * Caller*must*know*where*to*find*urn*val* Caller*and*Callee*run*on*same*CPU* *use*the*same*registers* So&how&do&we&deal&with&register&reuse?& The*convenJon*of*where*to*leave/find*things*is*called*the* calling*convenjon*(or*procedure*call*linkage).* Details&vary&between&systems& We&will&see&the&convenAon&for&IA32/Linux&in&detail& What&could&happen&if&our&program&didn t&follow&these&convenaons?* 11* 12*

4 Procedure*Control*Flow* Use*stack*to*support*procedure*call*and*urn* Procedure*call:*call label Push&urn&address&on&stack&(why?,(and(which(exact(address?)( Jump&to&label* Procedure*Control*Flow* Use*stack*to*support*procedure*call*and*urn* Procedure*call:*call label Push&urn&address&on&stack& Jump&to&label* Return*address:* Address&of&instrucAon&aLer&call* Example&from&disassembly:& e: e8 3d call 8048b90 <main> : 50 pushl %eax Return&address&=&* Procedure*urn:* Pop&urn&address&from&stack& Jump&to&address next*instrucjon* just*happens*to*be*a*push* could*be*anything* 13* 14* Procedure*Call* e: e8 3d call 8048b90 <main> : 50 pushl %eax Procedure*Call* e: e8 3d call 8048b90 <main> : 50 pushl %eax call 8048b90 call 8048b90 %eip 0x804854e %eip 0x804854e %eip 0x804854e %eip: program$counter 15* %eip: program$counter 16*

5 Procedure*Call* e: e8 3d call 8048b90 <main> : 50 pushl %eax Procedure*Return* : c3 call 8048b90 %eip 0x804854e %eip: program$counter %eip + 0x000063d 0x8048b90 relajve*address* just*like*jumps * (chosen&by&compiler;&& there s&also&an&absolute&call)& 17* %eip: program$counter %eip 0x * Procedure*Return* : c3 Procedure*Return* : c3 %eip 0x %eip 0x %eip 0x %eip 0x %eip: program$counter 19* %eip: program$counter 20*

6 Procedure*Return* : c3 %eip 0x %eip 0x Stack[Based*Languages* Languages*that*support*recursion* e.g.,&c,&java,&most&modern&languages& Code&must&be&re6entrant& MulAple&simultaneous&instanAaAons&of&single&procedure& Need&some&place&to&store&state&of&each&instanAaAon& Arguments& Local&variables& Return&pointer& discipline* State&for&a&given&procedure&needed&for&a&limited&Ame& StarAng&from&when&it&is&called&to&when&it&urns& Callee&always&urns&before&caller&does& allocated*in*frames$ State&for&a&single&procedure&instanAaAon& %eip: program$counter 21* 22* Call*Chain* ( ) (); ( ) (); (); ( ) (); Procedure****is*recursive* (calls*itself)* Call*Chain* Frames* Contents* Local&variables& FuncAon&arguments& Return&informaAon& Temporary&space& Frame*Pointer:** Pointer:** Management* Space&allocated&when&procedure&is&entered& SetYup &code& Space&deallocated&upon&urn& Finish &code& Previous* Frame* Frame* for* current* proc Top * 23* 24*

7 ( ) (); * * 25* ( ) (); (); * * 26* ( ) (); * * 27* ( ) (); * * 28*

8 ( ) (); * * 29* ( ) (); * * 30* ( ) (); * * 31* ( ) (); (); * * 32*

9 ( ) * * ( ) (); (); * * 33* 34* IA32/Linux*Frame* ( ) (); * * How*did*we*remember*where*to*point**when*urning?* 35* Current*Frame*( Top *to*bo^om)* Argument&build &area& (parameters&for&funcaon&& about&to&be&called)& Local&variables& (if&can t&be&kept&in&registers)& Saved&register&context&& (when&reusing&registers)& Old&frame&pointer&(for&caller)& Caller s*frame* Return&address& Pushed&by&call&instrucAon& Arguments&for&this&call& Frame*pointer* * pointer* * Caller* Frame* Arguments* Return*Addr* Old** Saved* Registers* +* Local* Variables* Argument* Build* 36*

10 RevisiJng*swap* int zip1 = 15213; int zip2 = 98195; void call_swap() swap(&zip1, &zip2); void swap(int *, int *) int t0 = *; int t1 = *; * = t1; * = t0; RevisiJng*swap* int zip1 = 15213; int zip2 = 98195; void call_swap() swap(&zip1, &zip2); void swap(int *, int *) int t0 = *; int t1 = *; * = t1; * = t0; Calling*swap*from*call_swap call_swap: pushl $zip2 # Global Var pushl $zip1 # Global Var call swap we*know*the*address* not*global*war!* 37* 38* RevisiJng*swap* RevisiJng*swap* int zip1 = 15213; int zip2 = 98195; void call_swap() swap(&zip1, &zip2); void swap(int *, int *) int t0 = *; int t1 = *; * = t1; * = t0; Calling*swap*from*call_swap call_swap: pushl $zip2 # Global Var pushl $zip1 # Global Var call swap * * &zip2 &zip1 ResulJng* void swap(int *, int *) int t0 = *; int t1 = *; * = t1; * = t0; swap: pushl movl, pushl %ebx movl 12(),%ecx movl 8(),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(),%ebx movl, popl Set* Up* Body* Finish* 39* 40*

11 swap*setup*#1* swap*setup*#1* Entering* ResulJng*Stack?* Entering* ResulJng* * * * * * * &zip2 &zip2 &zip1 &zip1 Old** swap: pushl movl, pushl %ebx Set* Up* swap: pushl movl, pushl %ebx Set* Up* 41* 42* swap*setup*#2* swap*setup*#3* Entering* ResulJng* Entering* ResulJng* * * * * * * * * &zip2 &zip2 &zip1 &zip1 swap: pushl movl, pushl %ebx Set* Up* Old** swap: pushl movl, pushl %ebx Set* Up* Old** Old*%ebx* 43* 44*

12 swap*body* swap*finish*#1* Entering* ResulJng* Finishing* ResulJng*Stack?* * * &zip2 &zip1 Offset$rela8ve$$ to$new$ 12* 8* 4* * * Old** Old*%ebx* * * Old** Old*%ebx* movl 12(),%ecx # get movl 8(),%edx # get... Body* 45* movl -4(),%ebx movl, popl Finish* 46* swap*finish*#1* swap*finish*#2* Finishing* ResulJng* Finishing* ResulJng* * * * * * * * * Old** Old*%ebx* Old** Old*%ebx* Old** Old*%ebx* Old** movl -4(),%ebx movl, popl Finish* ObservaJon:*Saved*and*restored** register*%ebx 47* movl -4(),%ebx movl, popl Finish* 48*

13 swap*finish*#3* swap*finish*#4* Finishing* ResulJng* Finishing* ResulJng* * * * * * * * * Old** Old*%ebx* Old** Old*%ebx* movl -4(),%ebx movl, popl Finish* 49* movl -4(),%ebx movl, popl Finish* 50* Disassembled*swap* swap*finish*#4* a4 <swap>: 80483a4: 55 push 80483a5: 89 e5 mov, 80483a7: 53 push %ebx 80483a8: 8b mov 0x8(),%edx 80483ab: 8b 4d 0c mov 0xc(),%ecx 80483ae: 8b 1a mov (%edx),%ebx 80483b0: 8b 01 mov (%ecx),%eax 80483b2: mov %eax,(%edx) 80483b4: mov %ebx,(%ecx) 80483b6: 5b pop %ebx mov 80483b7: c9 leave pop 80483b8: c3 Calling*Code* : e8 96 ff ff ff call 80483a4 <swap> e: 8b 45 f8 mov 0xfffffff8(),%eax relajve*address*(li^le*endian)*, 51* Finishing* * * Old** Old*%ebx* movl -4(),%ebx movl, popl Finish* ResulJng* * * ObservaJon* Saved&&&restored&register&%ebx* but&not&%eax,*%ecx,&or&%edx* 52*

14 Register*Saving*ConvenJons* When*procedure**calls*:* &is&the&caller* &is&the&callee$ Register*Saving*ConvenJons* When*procedure**calls*:* &is&the&caller* &is&the&callee& Can*a*register*be*used*for*temporary*storage?* : movl $45, %edx call?* addl %edx, %eax : movl 8(), %edx addl $98195, %edx Can*a*register*be*used*for*temporary*storage?* ConvenJons* Caller$Save $ Caller&saves&temporary&values&in&its&frame&before&calling& Callee$Save $ Callee&saves&temporary&values&in&its&frame&before&using& Contents&of&register&%edx*overwri]en&by&* 53* 54* IA32/Linux*Register*Usage* %eax, %edx, %ecx Caller&saves&prior&to&call&if& values&are&used&later& %eax also&used&to&urn& integer&value& %ebx, %esi, %edi Callee&saves&if&wants&to& use&them& Caller[Save* Temporaries* Callee[Save* Temporaries* Special* %eax %edx %ecx %ebx %esi %edi Example:*Pointers*to*Local*Variables* Top[Level*Call* int sfact(int x) int val = 1; s_helper(x, &val); urn val; sfact(3) * ** Recursive*Procedure* void s_helper (int x, int *accum) if (x <= 1) urn; else int z = *accum * x; *accum = z; s_helper (x-1,accum);, special&form&of&callee&save& &restored&to&original&values&upon&exit&from& procedure& Pass*pointer*to*update*locaJon* 55* 56*

15 Example:*Pointers*to*Local*Variables* Top[Level*Call* int sfact(int x) int val = 1; s_helper(x, &val); urn val; sfact(3) * *val*=*1* s_helper(3,*&val) ** Recursive*Procedure* void s_helper (int x, int *accum) if (x <= 1) urn; else int z = *accum * x; *accum = z; s_helper (x-1,accum); Pass*pointer*to*update*locaJon* Example:*Pointers*to*Local*Variables* Top[Level*Call* int sfact(int x) int val = 1; s_helper(x, &val); urn val; sfact(3) * *val*=*1* s_helper(3,*&val) *val*=*3* s_helper(2,*&val) ** Recursive*Procedure* void s_helper (int x, int *accum) if (x <= 1) urn; else int z = *accum * x; *accum = z; s_helper (x-1,accum); Pass*pointer*to*update*locaJon* 57* 58* Example:*Pointers*to*Local*Variables* Top[Level*Call* int sfact(int x) int val = 1; s_helper(x, &val); urn val; sfact(3) * *val*=*1* s_helper(3,*&val) *val*=*3* s_helper(2,*&val) *val*=*6* s_helper(1,*&val) *.* Recursive*Procedure* void s_helper (int x, int *accum) if (x <= 1) urn; else int z = *accum * x; *accum = z; s_helper (x-1,accum); Pass*pointer*to*update*locaJon* CreaJng*&*IniJalizing*Pointer* int sfact(int x) int val = 1; s_helper(x, &val); urn val; IniJal*part*of*sfact* _sfact: pushl # Save movl, # Set subl $16, # Add 16 bytes movl 8(),%edx # edx = x movl $1,-4() # val = 1 Variable*val*must*be*stored*on*stack* Because:&Need&to&create&pointer&to&it& Compute*pointer*as*-4()* Push*on*stack*as*second*argument* 8 4 x 0 Old** -4 val = Temp.* Unused* Space* 59* 60*

16 Passing*Pointer* int sfact(int x) int val = 1; s_helper(x, &val); urn val; Calling*s_helper*from*sfact* at*jme*of*call:* leal -4(),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(),%eax # Return val # Finish Variable*val*must*be*stored*on*stack* Because:&Need&to&create&pointer&to&it& Compute*pointer*as*-4()* Push*on*stack*as*second*argument* x Old** -4 val=x! = Unused* &val x IA*32*Procedure*Summary* Important*points:* IA32&procedures&are&a&combinaAon&of&instruc8ons& and&conven8ons( Caller* ConvenAons&prevent&funcAons&from& Frame* disrupang&each&other& Stack&is&the&right&data&structure&for&procedure& call&/&urn& * If&P&calls&Q,&then&Q&urns&before&P& Recursion*handled*by*normal*calling* convenjons* Can&safely&store&values&in&local&stack&frame&and&in& calleeysaved&registers& Put&funcAon&arguments&at&top&of&stack& Result&urned&in&%eax * Arguments* Return*Addr* Old** Saved* Registers* +* Local* Variables* Argument* Build* 61* 62* x86[64*procedure*calling*convenjon* x86[64*64[bit*registers:*usage*convenjons* Doubling*of*registers*makes*us*less*dependent*on*stack* Store&argument&in&registers& Store&temporary&variables&in&registers& %rax %rbx Return*value* Callee*saved* %r8 %r9 Argument*#5* Argument*#6* What*do*we*do*if*we*have*too*many*arguments*or*too*many* temporary*variables? * ** %rcx %rdx Argument*#4* Argument*#3* %r10 %r11 Caller*saved* Caller*Saved* %rsi Argument*#2* %r12 Callee*saved* %rdi Argument*#1* %r13 Callee*saved* %rsp pointer* %r14 Callee*saved* %rbp Callee*saved* %r15 Callee*saved* 63* 64*

17 RevisiJng*swap,*IA32*vs.*x86[64*versions* X86[64*procedure*call*highlights* swap: pushl movl, pushl %ebx movl 12(),%ecx movl 8(),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(),%ebx movl, popl Set* Up* Body* Finish* swap (64-bit long ints): movq (%rdi), %rdx movq (%rsi), %rax movq %rax, (%rdi) movq %rdx, (%rsi) Arguments*passed*in*registers* First&()&in&%rdi,&& second&()&in&%rsi 64Ybit&pointers& No*stack*operaJons** required*(except*)* Avoiding*stack* Can&hold&all&local&informaAon&& in&registers& 65* Arguments*(up*to*first*6)*in*registers* Faster&to&get&these&values&from&registers&than&from&stack&in&memory& Local*variables*also*in*registers*(if*there*is*room)* Registers*sJll*designated* caller[saved *or* callee[saved * callq*instrucjon*stores*64[bit*urn*address*on*stack** Address&pushed&onto&stack,&decremenAng&%rsp&by&8& No*frame*pointer* All&references&to&stack&frame&made&relaAve&to&%rsp;&eliminates&need&to& update&/%rbp,&which&is&now&available&for&generalypurpose&use& FuncJons*can*access*memory*up*to*128*bytes*beyond*%rsp:* the* red*zone * Can&store&some&temps&on&stack&without&altering&%rsp& 66* x86[64*memory*layout* 2 N -1 Stack Dynamic Data (Heap) Static Data Literals %rsp* 128[byte*red*zone* space(lower(than(the( stack(pointer(that( procedures(can(use(for( data(not(needed(across( procedure(calls( op8miza8on(to(avoid( extra(%rsp(updates( x86[64*frames* Owen*(ideally),*x86[64*funcJons*need*no*stack*frame*at*all* Just&a&urn&address&is&pushed&onto&the&stack&when&a&funcAon&call&is& made& A*funcJon*does*need*a*stack*frame*when*it:* Has&too&many&local&variables&to&hold&in&registers& Has&local&variables&that&are&arrays&or&structs& Uses&the&addressYof&operator&(&)&to&compute&the&address&of&a&local& variable& Calls&another&funcAon&that&takes&more&than&six&arguments& Needs&to&save&the&state&of&callerYsave&registers&before&calling&a&procedure& Needs&to&save&the&state&of&calleeYsave&registers&before&modifying&them& 0 Instructions 67* 68*

18 long int call_proc() long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); urn (x1+x2)*(x3-x4); call_proc: subq $32,%rsp movq $1,16(%rsp) movl $2,24(%rsp) movw $3,28(%rsp) movb $4,31(%rsp) long int call_proc() long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); urn (x1+x2)*(x3-x4); call_proc: subq $32,%rsp movq $1,16(%rsp) movl $2,24(%rsp) movw $3,28(%rsp) movb $4,31(%rsp) Return*address*to*caller*of*call_proc* %rsp Return*address*to*caller*of*call_proc* x4* x3* x2* x1* NB:*Details*may*vary* depending*on*compiler.* %rsp 69* 70* long int call_proc() long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); urn (x1+x2)*(x3-x4); Return*address*to*caller*of*call_proc* x4* x3* x2* call_proc: leaq 24(%rsp),%rcx leaq 16(%rsp),%rsi leaq 31(%rsp),%rax movq %rax,8(%rsp) movl $4,(%rsp) leaq 28(%rsp),%r9 movl $3,%r8d movl $2,%edx movq $1,%rdi call proc long int call_proc() long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); urn (x1+x2)*(x3-x4); Return*address*to*caller*of*call_proc* x4* x3* x2* call_proc: leaq 24(%rsp),%rcx leaq 16(%rsp),%rsi leaq 31(%rsp),%rax movq %rax,8(%rsp) movl $4,(%rsp) leaq 28(%rsp),%r9 movl $3,%r8d movl $2,%edx movq $1,%rdi call proc note* sizes* x1* Arg*8* Arg*7* %rsp Arguments*passed*in*(in* order):*rdi,*rsi,*rdx,*rcx,*r8,*r9* x1* Arg*8* Arg*7* %rsp Arguments*passed*in*(in* order):*rdi,*rsi,*rdx,*rcx,*r8,*r9* 71* 72*

19 sign*extension!* long int call_proc() long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); urn (x1+x2)*(x3-x4); Return*address*to*caller*of*call_proc* x4* x3* x2* call_proc: leaq 24(%rsp),%rcx leaq 16(%rsp),%rsi leaq 31(%rsp),%rax movq %rax,8(%rsp) movl $4,(%rsp) leaq 28(%rsp),%r9 movl $3,%r8d movl $2,%edx movq $1,%rdi call proc long int call_proc() long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); urn (x1+x2)*(x3-x4); Return*address*to*caller*of*call_proc* x4* x3* x2* call_proc: movswl 28(%rsp),%eax movsbl 31(%rsp),%edx subl %edx,%eax cltq movslq 24(%rsp),%rdx addq 16(%rsp),%rdx imulq %rdx,%rax addq $32,%rsp x1* x1* Arg*8* Arg*7* Arg*8* Arg*7* %rsp Return*address*to*line*awer*call*to*proc* %rsp 73* 74* long int call_proc() long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); urn (x1+x2)*(x3-x4); Return*address*to*caller*of*call_proc* call_proc: movswl 28(%rsp),%eax movsbl 31(%rsp),%edx subl %edx,%eax cltq movslq 24(%rsp),%rdx addq 16(%rsp),%rdx imulq %rdx,%rax addq $32,%rsp %rsp x86[64*procedure*summary* Heavy*use*of*registers*(faster*than*using*stack*in*memory)* Parameter&passing& More&temporaries&since&more&registers& Minimal*use*of*stack* SomeAmes&none& When&needed,&allocate/deallocate&enAre&frame&at&once& No&more&frame&pointer:&address&relaAve&to&stack&pointer& More*room*for*compiler*opJmizaJons* Prefer&to&store&data&in&registers&rather&than&memory& Minimize&modificaAons&to&stack&pointer& 75* 76*

University of Washington

University of Washington Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp

More information

Procedures and the Call Stack

Procedures and the Call Stack Procedures and the Call Stack Topics Procedures Call stack Procedure/stack instructions Calling conventions Register-saving conventions Why Procedures? Why functions? Why methods? int contains_char(char*

More information

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element Machine Representa/on of Programs: Procedures Instructors: Sanjeev Se(a 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoGom Increasing Addresses Register

More information

Machine-level Programming (3)

Machine-level Programming (3) Machine-level Programming (3) Procedures A: call A call A return Two issues How to return to the correct position? How to pass arguments and return values between callee to caller? 2 Procedure Control

More information

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables class07.ppt

More information

Machine Programming 3: Procedures

Machine Programming 3: Procedures Machine Programming 3: Procedures CS61, Lecture 5 Prof. Stephen Chong September 15, 2011 Announcements Assignment 2 (Binary bomb) due next week If you haven t yet please create a VM to make sure the infrastructure

More information

X86 Assembly -Procedure II:1

X86 Assembly -Procedure II:1 X86 Assembly -Procedure II:1 IA32 Object Code Setup Label.L61 becomes address 0x8048630 Label.L62 becomes address 0x80488dc Assembly Code switch_eg:... ja.l61 # if > goto default jmp *.L62(,%edx,4) # goto

More information

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung Assembly III: Procedures Jo, Heeseung IA-32 Stack (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline

More information

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping Lecture 5 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows

More information

Machine Program: Procedure. Zhaoguo Wang

Machine Program: Procedure. Zhaoguo Wang Machine Program: Procedure Zhaoguo Wang Requirements of procedure calls? P() { y = Q(x); y++; 1. Passing control int Q(int i) { int t, z; return z; Requirements of procedure calls? P() { y = Q(x); y++;

More information

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing CISC 36 Machine-Level Programming III: Procedures Sept. 22, 2 IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element

More information

Machine-Level Programming III: Procedures

Machine-Level Programming III: Procedures Machine-Level Programming III: Procedures IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element Bottom Increasing

More information

CS213. Machine-Level Programming III: Procedures

CS213. Machine-Level Programming III: Procedures CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows toward

More information

Sungkyunkwan University

Sungkyunkwan University Switch statements IA 32 Procedures Stack Structure Calling Conventions Illustrations of Recursion & Pointers long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case

More information

Machine- Level Programming III: Switch Statements and IA32 Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures Machine- Level Programming III: Switch Statements and IA32 Procedures CS 485: Systems Programming Fall 2015 Instructor: James Griffioen Adapted from slides by R. Bryant and D. O Hallaron (hjp://csapp.cs.cmu.edu/public/instructors.html)

More information

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region

More information

The Hardware/Software Interface CSE351 Spring 2015

The Hardware/Software Interface CSE351 Spring 2015 The Hardware/Software Interface CSE351 Spring 2015 Lecture 7 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang

More information

Giving credit where credit is due

Giving credit where credit is due CSCE 230J Computer Organization Machine-Level Programming III: Procedures Dr. Steve Goddard goddard@cse.unl.edu Giving credit where credit is due Most of slides for this lecture are based on slides created

More information

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address IA32 Procedures 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoDom Increasing Addresses Register contains lowest stack address address of top element Stack

More information

Assembly Programming IV

Assembly Programming IV Assembly Programming IV CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis 1 Administrivia Homework 2 due

More information

University*of*Washington*

University*of*Washington* Roadmap* C:* car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly* language:* Machine* code:* Computer* system:* get_mpg: pushq movq... popq ret %rbp %rsp,

More information

x86 Programming I CSE 351 Winter

x86 Programming I CSE 351 Winter x86 Programming I CSE 351 Winter 2017 http://xkcd.com/409/ Administrivia Lab 2 released! Da bomb! Go to section! No Luis OH Later this week 2 Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals

More information

Data Representa/ons: IA32 + x86-64

Data Representa/ons: IA32 + x86-64 X86-64 Instruc/on Set Architecture Instructor: Sanjeev Se(a 1 Data Representa/ons: IA32 + x86-64 Sizes of C Objects (in Bytes) C Data Type Typical 32- bit Intel IA32 x86-64 unsigned 4 4 4 int 4 4 4 long

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 CS429 Slideset 9: 1 Mechanisms in Procedures

More information

Systems Programming and Computer Architecture ( )

Systems Programming and Computer Architecture ( ) Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 AS 2016 Compiling C Control Flow 1 8: Compiling C

More information

Machine-level Programs Procedure

Machine-level Programs Procedure Computer Systems Machine-level Programs Procedure Han, Hwansoo Mechanisms in Procedures Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value

More information

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 Mechanisms in Procedures Passing Control

More information

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Mechanisms in Procedures Passing control To beginning of procedure code

More information

x86-64 Programming III

x86-64 Programming III x86-64 Programming III CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan http://xkcd.com/1652/ Administrivia Homework 2 due Wednesday (7/11) Lab

More information

Machine-Level Programming III: Procedures

Machine-Level Programming III: Procedures Machine-Level Programming III: Procedures CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGİN Slides adapted from Bryant & O Hallaron s slides Mechanisms in Procedures Passing control

More information

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker Procedure Call Registers Return %rax %eax %r8 %r8d Arg 5 %rbx %ebx %r9 %r9d Arg 6 Arg 4 %rcx %ecx %r10 %r10d Arg 3 %rdx %edx %r11 %r11d Arg 2 %rsi %esi %r12 %r12d Arg 1 %rdi %edi %r13 %r13d ptr %esp %r14

More information

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon Carnegie Mellon Machine-Level Programming III: Procedures 15-213/18-213/14-513/15-513: Introduction to Computer Systems 7 th Lecture, September 18, 2018 Today Procedures Mechanisms Stack Structure Calling

More information

x86-64 Programming III & The Stack

x86-64 Programming III & The Stack x86-64 Programming III & The Stack CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan http://xkcd.com/1652/ Administrative

More information

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/409/

More information

Building an Executable

Building an Executable Building an Executable CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan http://xkcd.com/1790/ Administrivia Lab 2 due Monday (7/16) Homework 3 due

More information

An Experience Like No Other. Stack Discipline Aug. 30, 2006

An Experience Like No Other. Stack Discipline Aug. 30, 2006 15-410 An Experience Like No Other Discipline Aug. 30, 2006 Bruce Maggs Dave Eckhardt Slides originally stolen from 15-213 15-410, F 06 Synchronization Registration If you're here but not registered, please

More information

hnp://

hnp:// The bots face off in a tournament against one another and about an equal number of humans, with each player trying to score points by elimina&ng its opponents. Each player also has a "judging gun" in addi&on

More information

Assembly Programming IV

Assembly Programming IV Assembly Programming IV CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui The Data That Turned the World Upside Down The company

More information

The Stack & Procedures

The Stack & Procedures The Stack & Procedures CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Homework 2 due

More information

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames 6.1 CS356 Unit 6 x86 Procedures Basic Stack Frames 6.2 Review of Program Counter (Instruc. Pointer) PC/IP is used to fetch an instruction PC/IP contains the address of the next instruction The value in

More information

Stack Discipline Jan. 19, 2018

Stack Discipline Jan. 19, 2018 15-410 An Experience Like No Other Discipline Jan. 19, 2018 Dave Eckhardt Brian Railing Slides originally stolen from 15-213 1 15-410, S 18 Synchronization Registration The wait list will probably be done

More information

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp Stack Frame Components Frame pointer %rbp Stack pointer Caller Frame rguments 7+ Return ddr Old %rbp Saved Registers + Local Variables rgument Build 1 Using the Stack (4) Stack Structure long call_incr()

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent

More information

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Arrays. CSE 351 Autumn Instructor: Justin Hsia rrays CSE 351 utumn 2016 Instructor: Justin Hsia Teaching ssistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/1513/

More information

The Stack & Procedures

The Stack & Procedures The Stack & Procedures CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/648/

More information

Assembly I: Basic Operations. Jo, Heeseung

Assembly I: Basic Operations. Jo, Heeseung Assembly I: Basic Operations Jo, Heeseung Moving Data (1) Moving data: movl source, dest Move 4-byte ("long") word Lots of these in typical code Operand types Immediate: constant integer data - Like C

More information

CS 107 Lecture 10: Assembly Part I

CS 107 Lecture 10: Assembly Part I CS 107 Lecture 10: Assembly Part I Friday, February 9th, 2018 Computer Systems Winter 2018 Stanford University Computer Science Department Reading: Course Reader: x86-64 Assembly Language, Textbook: Chapter

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung ASSEMBLY I: BASIC OPERATIONS Jo, Heeseung MOVING DATA (1) Moving data: movl source, dest Move 4-byte ("long") word Lots of these in typical code Operand types Immediate: constant integer data - Like C

More information

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc -S) text Asm

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson Structs and Alignment CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Lab 2 due TONIGHT

More information

CS241 Computer Organization Spring 2015 IA

CS241 Computer Organization Spring 2015 IA CS241 Computer Organization Spring 2015 IA-32 2-10 2015 Outline! Review HW#3 and Quiz#1! More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add,

More information

Structs and Alignment CSE 351 Spring

Structs and Alignment CSE 351 Spring Structs and Alignment CSE 351 Spring 2018 http://xkcd.com/1168/ Administrivia Homework 3 due Wednesday Lab 3 released, due next week Lab 2 and midterm will be graded this week [in that order] 2 Roadmap

More information

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT CS33 Intro to Computer Systems XI 1 Copyright 2017 Thomas

More information

Machine/Assembler Language Putting It All Together

Machine/Assembler Language Putting It All Together COMP 40: Machine Structure and Assembly Language Programming Fall 2015 Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent instructions and conventions 2 Function Call Problems (1) Calling and returning

More information

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Roadmap. Java: Assembly language: OS: Machine code: Computer system: Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp

More information

Machine- Level Programming III: Switch Statements and IA32 Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures Machine- Level Programming III: Switch Statements and IA32 Procedures 15-213: Introduc;on to Computer Systems 6 th Lecture, Sep. 9, 2010 Instructors: Randy Bryant and Dave O Hallaron Today Switch statements

More information

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Arrays. CSE 351 Autumn Instructor: Justin Hsia rrays CSE 351 utumn 2017 Instructor: Justin Hsia Teaching ssistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/1270/ dministrivia

More information

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor CS 261 Fall 2018 0000000100000f50 55 48 89 e5 48 83 ec 10 48 8d 3d 3b 00 00 00 c7 0000000100000f60 45 fc 00 00 00 00 b0 00 e8 0d 00 00 00 31 c9 89 0000000100000f70 45 f8 89 c8 48 83 c4 10 5d c3 Mike Lam,

More information

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2014 Midterm Exam (29 October 2014) CSE351 Autumn 2014 Midterm Exam (29 October 2014) Please read through the entire examination first! We designed this exam so that it can be completed in 50 minutes and, hopefully, this estimate will prove

More information

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia Structs & Alignment CSE 351 Autumn 2018 Instructor: Justin Hsia Teaching Assistants: Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie

More information

Areas for growth: I love feedback

Areas for growth: I love feedback Assembly part 2 1 Areas for growth: I love feedback Speed, I will go slower. Clarity. I will take time to explain everything on the slides. Feedback. I give more Kahoot questions and explain each answer.

More information

Function Calls and Stack

Function Calls and Stack Function Calls and Stack Philipp Koehn 16 April 2018 1 functions Another Example 2 C code with an undefined function int main(void) { int a = 2; int b = do_something(a); urn b; } This can be successfully

More information

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University Assembly I: Basic Operations Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Moving Data (1) Moving data: movl source, dest Move 4-byte ( long )

More information

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CSE 351 Midterm Exam Spring 2016 May 2, 2015 Name: CSE 351 Midterm Exam Spring 2016 May 2, 2015 UWNetID: Solution Please do not turn the page until 11:30. Instructions The exam is closed book, closed notes (no calculators, no mobile phones, no laptops,

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron

More information

x86-64 Programming I CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan

x86-64 Programming I CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan x86-64 Programming I CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan http://www.smbc-comics.com/?id=2999 Administrivia Lab 1b due on Thursday (7/5)

More information

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly I: Basic Operations Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Basic Execution Environment RAX RBX RCX RDX RSI RDI RBP RSP R8 R9 R10

More information

Systems I. Machine-Level Programming V: Procedures

Systems I. Machine-Level Programming V: Procedures Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

More information

Today: Machine Programming I: Basics

Today: Machine Programming I: Basics Today: Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics: Registers, operands, move Intro to x86-64 1 Intel x86 Processors Totally dominate

More information

Structs and Alignment

Structs and Alignment Structs and Alignment CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun

More information

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia Executables & rrays CSE 351 utumn 2018 Instructor: Justin Hsia Teaching ssistants: kshat ggarwal n Wang ndrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie Tian Teagan

More information

Machine-Level Programming (2)

Machine-Level Programming (2) Machine-Level Programming (2) Yanqiao ZHU Introduction to Computer Systems Project Future (Fall 2017) Google Camp, Tongji University Outline Control Condition Codes Conditional Branches and Conditional

More information

1 Number Representation(10 points)

1 Number Representation(10 points) Name: Sp15 Midterm Q1 1 Number Representation(10 points) 1 NUMBER REPRESENTATION(10 POINTS) Let x=0xe and y=0x7 be integers stored on a machine with a word size of 4bits. Show your work with the following

More information

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe Machine Level Programming I: Basics 15 213/18 213: 213: Introduction to Computer Systems 5 th Lecture, Jan. 31, 2012 Instructors: Todd C. Mowry & Anthony Rowe 1 Today: Machine Programming gi: Basics History

More information

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

1 /* file cpuid2.s */ 4.asciz The processor Vendor ID is %s \n 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start. 1 /* file cpuid2.s */ 2.section.data 3 output: 4.asciz "The processor Vendor ID is %s \n" 5.section.bss 6.lcomm buffer, 12 7.section.text 8.globl _start 9 _start: 10 movl $0, %eax 11 cpuid 12 movl $buffer,

More information

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2014 Midterm Exam (29 October 2014) CSE351 Autumn 2014 Midterm Exam (29 October 2014) (Version A) Please read through the entire examination first! We designed this exam so that it can be completed in 50 minutes and, hopefully, this estimate

More information

Process Layout and Function Calls

Process Layout and Function Calls Process Layout and Function Calls CS 6 Spring 07 / 8 Process Layout in Memory Stack grows towards decreasing addresses. is initialized at run-time. Heap grow towards increasing addresses. is initialized

More information

Machine Level Programming I: Basics

Machine Level Programming I: Basics Carnegie Mellon Machine Level Programming I: Basics Kai Shen Why do I care for machine code? Chances are, you ll never write programs in machine code Compilers are much better & more patient than you are

More information

CPEG421/621 Tutorial

CPEG421/621 Tutorial CPEG421/621 Tutorial Compiler data representation system call interface calling convention Assembler object file format object code model Linker program initialization exception handling relocation model

More information

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION Today: Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics:

More information

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012 6.172 Performance Engineering of Software Systems SPEED LIMIT PER ORDER OF 6.172 LECTURE 3 C to Assembly I-Ting Angelina Lee September 13, 2012 2012 Charles E. Leiserson and I-Ting Angelina Lee 1 Bugs

More information

Structs and Alignment

Structs and Alignment Structs and Alignment CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui Self Driving Cars Will Make Organ Shortages Even Worse

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Programming

More information

Do not turn the page until 5:10.

Do not turn the page until 5:10. University of Washington Computer Science & Engineering Autumn 2018 Instructor: Justin Hsia 2018-10-29 Last Name: First Name: Student ID Number: Name of person to your Left Right All work is my own. I

More information

Machine- Level Representa2on: Procedure

Machine- Level Representa2on: Procedure Machine- Level Representa2on: Procedure CSCI 2021: Machine Architecture and Organiza2on Pen- Chung Yew Department Computer Science and Engineering University of Minnesota With Slides from Bryant, O Hallaron

More information

The Hardware/Software Interface CSE351 Spring 2013

The Hardware/Software Interface CSE351 Spring 2013 The Hardware/Software Interface CSE351 Spring 2013 x86 Programming II 2 Today s Topics: control flow Condition codes Conditional and unconditional branches Loops 3 Conditionals and Control Flow A conditional

More information

Lecture 4 CIS 341: COMPILERS

Lecture 4 CIS 341: COMPILERS Lecture 4 CIS 341: COMPILERS CIS 341 Announcements HW2: X86lite Available on the course web pages. Due: Weds. Feb. 7 th at midnight Pair-programming project Zdancewic CIS 341: Compilers 2 X86 Schematic

More information

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective void P() { y = Q(x); print(y); return;... int Q(int t) { int v[10]; return v[t]; Computer Systems: A Programmer s Perspective %rax %rbx 0x101 0x41 0x7FFFFA8 0x1 0x7FFFFF8 0xB5A9 0x7FFFFF0 0x789ABC 0x7FFFFE8

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Intel x86

More information

X86 Stack Calling Function POV

X86 Stack Calling Function POV X86 Stack Calling Function POV Computer Systems Section 3.7 Stack Frame Reg Value ebp xffff FFF0 esp xffff FFE0 eax x0000 000E Memory Address Value xffff FFF8 xffff FFF4 x0000 0004 xffff FFF4 x0000 0003

More information

15-213/18-243, Spring 2011 Exam 1

15-213/18-243, Spring 2011 Exam 1 Andrew login ID: Full Name: Section: 15-213/18-243, Spring 2011 Exam 1 Thursday, March 3, 2011 (v1) Instructions: Make sure that your exam is not missing any sheets, then write your Andrew login ID, full

More information

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack bottom.. Earlier Frames. Frame for calling function P. Increasing address CS 107 Stack "bottom" Earlier Frames Lecture 13: Assembly Part III Argument n Friday, November 10, 2017 Computer Systems Increasing address Argument 7 Frame for calling function P Fall 2017 Stanford University

More information

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information