forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-assembly-64bit.html
1769 lines (1572 loc) · 64.9 KB
/
08-assembly-64bit.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CS 2150: 08-x86 (64 bit) slide set</title>
<meta name="description" content="A set of slides for a course on Program and Data Representation">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="../slides/reveal.js/css/reveal.css">
<link rel="stylesheet" href="../slides/reveal.js/css/theme/black.css" id="theme">
<link rel="stylesheet" href="../slides/css/pdr.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="../slides/reveal.js/lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? '../slides/reveal.js/css/print/pdf.css' : '../slides/reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="../slides/reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<script type="text/javascript" src="../slides/js/dhtmlwindow.js"></script>
<script type="text/javascript" src="../slides/js/canvas.js"></script>
<link rel="stylesheet" href="../slides/css/dhtmlwindow.css" type="text/css">
<style>.reveal li { font-size:93%; line-height:120%; }</style>
</head>
<body onload="canvasinit()">
<div id="dhtmlwindowholder"><span style="display:none"></span></div>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-markdown id="cover"><script type="text/template">
# CS 2150
### Program and Data Representation
<center><small><a href="http://www.cs.virginia.edu/~asb">Aaron Bloomfield</a> (aaron@virginia.edu)</small></center>
<center><small><a href="http://@github/uva-cs/pdr">@github</a> | <a href="index.html">↑</a> | <a href="daily-announcements.html?print-pdf"><img class="print" width="20" src="../slides/images/print-icon.png"></a></small></center>
## 64 bit x86 (assembly language)
</script></section>
<section>
<h2>CS 2150 Roadmap</h2>
<table class="wide">
<tr><td colspan="3"><p class="center">Data Representation</p></td><td></td><td colspan="3"><p class="center">Program Representation</p></td></tr>
<tr>
<td class="top"><small> <br> <br>string<br> <br> <br> <br>int x[3]<br> <br> <br> <br>char x<br> <br> <br> <br>0x9cd0f0ad<br> <br> <br> <br>01101011</small></td>
<!-- image adapted from http://openclipart.org/detail/3677/arrow-left-right-by-torfnase -->
<td><img class="noborder" src="images/red-double-arrow.png" height="500" alt="vertical red double arrow"></td>
<td class="top"> <br>Objects<br> <br>Arrays<br> <br>Primitive types<br> <br>Addresses<br> <br>bits</td>
<td> </td>
<td class="top"><small> <br> <br>Java code<br> <br> <br>C++ code<br> <br> <br>C code<br> <br> <br>x86 code<br> <br> <br>IBCM<br> <br> <br>hexadecimal</small></td>
<!-- image adapted from http://openclipart.org/detail/3677/arrow-left-right-by-torfnase -->
<td><img class="noborder" src="images/green-double-arrow.png" height="500" alt="vertical green double arrow"></td>
<td class="top"> <br>High-level language<br> <br>Low-level language<br> <br>Assembly language<br> <br>Machine code</td>
</tr>
</table>
</section>
<section data-markdown><script type="text/template">
# Contents
[Introduction to x86](#/introduction)
[x86 Instruction Set](#/x86insts)
[Calling Conventions](#/callingconv)
[Callee Rules](#/callee)
[Caller Rules](#/caller)
[Activation Records](#/actrecs)
[x86 Examples](#/examples)
</script></section>
<section>
<section id="introduction" data-markdown><script type="text/template">
# Introduction to x86
</script></section>
<section>
<h2>History of x86</h2>
<p><a href="https://en.wikipedia.org/wiki/File:C4004_%28Intel%29.jpg"><img alt="Intel 4004" src="images/08-x86/C4004.jpg"></a></p>
<table class="transparent"><tr><td>
<ul>
<li>1971: 4004, 4-bit words</li>
<li>1972: 8008, 8-bit words</li>
<li>1978: 8086, 16-bit words</li>
<li>1982: 80286</li>
<li>1985: 80386, 32-bit words</li>
<li>1989: 80486</li>
<li>1993: Pentium</li>
<li>1995: Pentium Pro</li>
</ul>
</td><td>
<ul>
<li>1997: Pentium II</li>
<li>1998: Pentium III</li>
<li>2000-2008: Pentium IV</li>
<li>2003: AMD64 Opteron</li>
<li>2004: Intel 64 bit chips</li>
<li>2005-2008: Pentium D</li>
<li>2006-2011: Core 2</li>
<li>2008-present: Core i3, i5, i7</li>
</ul>
</td></tr></table>
</section>
<section data-markdown><script type="text/template">
## AMD vs Intel
- AMD developed their standard in 2000
- First chip was available in 2003
- Intel had been working on their IA64 (Itanium) line
- There is a reason you have never heard of it
- They released their first chip, similar to the AMD64, in 2004
- But it had slight differences!
- The x86-64 standard is what compilers typically use, and is the intersection of the two instruction sets
- More than 95% of it is the same
</script></section>
<section>
<h2>IBCM vs. x86: Registers</h2>
<img src="images/08-x86/registers_x86_64bit.png" class="stretch">
</section>
<section data-markdown><script type="text/template">
## Funny register names
| 8-bit name | 16-bit name | 32-bit name | 64-bit name |
|--|--|--|--|
| a (al/ah) | ax | eax | rax |
| b (bl/bh) | bx | ebx | rbx |
| c (cl/ch) | cx | ecx | rcx |
| d (dl/dh) | dx | edx | rdx |
| - | - | esi | rsi |
| - | - | edi | rdi |
| - | sp | esp | rsp |
| - | bp | ebp | rbp |
| - | - | - | r8 - r15 |
</script></section>
<section data-markdown><script type="text/template">
## IBCM vs. x86: Fetch Execute Cycle (same)
```
while(power is on) {
IR := mem[PC]
PC := PC + 1 (word) // 64-bits in x86
execute instruction in IR
}
```
- PC = program counter
- IR = instruction register
</script></section>
<section>
<h2>Declaring Variables in x86</h2>
<table class="transparent"><tr><td>
<p>Directives</p>
<ul>
<li>byte: 1 byte (DB) declare byte</li>
<li>word: 2 bytes (DW)</li>
<li>double: 4 bytes (DD)</li>
<li>quadword: 8 bytes (DQ)</li>
</ul>
<p> </p>
<p><code>TIMES x DB 0</code> directive means create x bytes of value zero</p>
</td><td>
<pre><code>section .data
a DB 23
b DW ?
c DD 3000
d DQ -800
x DD 1, 2, 3
y TIMES 8 DB 0
str DB 'hello', 0
z TIMES 50 DD ?</code></pre>
</td></tr></table>
</section>
<section data-markdown><script type="text/template">
## mov command
- `mov <dest>, <src>`
- Where dest and src can be:
- A register
- A constant
- Variable name
- Pointer: [rbx]
- *You will often see movq (move quad word) or movl (move double word), etc.
- This is more precise as you are declaring how many bytes will be moved, but depends on the assembly syntax used (more on this later)
</script></section>
<section>
<h2>Addressing Memory</h2>
<table class="transparent"><tr><td class="top">
<ul>
<li>Up to <i>2 registers</i> and one <i>64-bit signed constant</i> can be added together to compute a memory address<br> </li>
<li>Furthermore, one register can be pre-multiplied by 2, 4, or 8<ul>
<li>word-align</li>
<li>double-align</li>
<li>quadword-align</li>
</ul></li></ul>
</td><td style="width:50px"></td><td class="top">
<pre><code>mov rax, rbx
mov rax, [rbx]
mov [var], rbx
mov rax, [r13 - 4]
mov [rsi + rax], cl
mov rdx, [rsi + 4*rbx]</code></pre>
<p> </p>
<p>Incorrect: (why?)</p>
<pre><code>mov rax, [r11 - rcx]
mov [rax + r5 + rdi], rbx
mov [4*rax + 2*rbx], rcx</code></pre>
</td></tr></table>
</section>
<section>
<table class="transparent"><tr><td class="top">
<table class="transparent">
<tr><td class="top" colspan="3"><h2>Example</h2></td></tr>
<tr><td class="top"><p class="center">Source code:</p><pre><code>mov rcx, rax
mov rdx, [rbx]
mov rsi, [rdx+rax+16]
mov [rsi], 45
mov [a], 15
lea rdi, [a]</code></pre></td>
<td style="width:50px"></td>
<td class="top">
<p class="center">Registers:</p>
<table class="transparent">
<tr><td>rax</td><td class="border" style="width:100px">100</td></tr>
<tr><td>rbx</td><td class="border" style="width:100px">108</td></tr>
<tr><td>rcx</td><td class="border" style="width:100px"><span class="fragment" data-fragment-index="1">100</span></td></tr>
<tr><td>rdx</td><td class="border" style="width:100px"><span class="fragment" data-fragment-index="2">8</span></td></tr>
<tr><td>rsi</td><td class="border" style="width:100px"><span class="fragment" data-fragment-index="3">200</span></td></tr>
<tr><td>rdi</td><td class="border" style="width:100px;border-bottom:medium solid;"><span class="fragment" data-fragment-index="6">300</span></td></tr>
<tr><td>r8</td><td class="border" style="width:100px;border-bottom:medium solid;"></td></tr>
<tr><td>...</td><td class="border" style="width:100px;border-bottom:medium solid;"></td></tr>
</table>
</td></tr></table>
</td><td style="width:50px"></td><td class="top">
<p class="center">Memory:</p>
<table class="transparent">
<tr><td>100</td><td class="border" style="width:100px"></td></tr>
<tr><td>108</td><td class="border" style="width:100px">8</td></tr>
<tr><td>116</td><td class="border" style="width:100px"></td></tr>
<tr><td>124</td><td class="border" style="width:100px">200</td></tr>
<tr><td>132</td><td class="border" style="width:100px"></td></tr>
<tr><td>...</td><td class="border" style="width:100px"></td></tr>
<tr><td>200</td><td class="border" style="width:100px"><span class="fragment" data-fragment-index="4">45</span></td></tr>
<tr><td>208</td><td class="border" style="width:100px"></td></tr>
<tr><td>...</td><td class="border" style="width:100px"></td></tr>
<tr><td>a: 300</td><td class="border" style="width:100px"><span class="fragment" data-fragment-index="5">15</span></td></tr>
<tr><td>308</td><td class="border" style="width:100px"></td></tr>
<tr><td>...</td><td class="border" style="width:100px;border-bottom:medium solid;"></td></tr>
</table>
</td></tr></table>
</section>
<section data-markdown><script type="text/template">
## `mov rax, [4*rsi-rdx]`
1. Valid
2. Invalid
3. Not sure
</script></section>
<section data-markdown><script type="text/template">
## `mov rax, [4*rsi+4]`
1. Valid
2. Invalid
3. Not sure
</script></section>
<section data-markdown><script type="text/template">
## `mov rax, [4*rsi+rdx+8]`
1. Valid
2. Invalid
3. Not sure
</script></section>
<section data-markdown><script type="text/template">
## `mov rax, [rsi+4*rdx]`
1. Valid
2. Invalid
3. Not sure
</script></section>
<section data-markdown><script type="text/template">
## `mov rax+8, [rsi]`
1. Valid
2. Invalid
3. Not sure
</script></section>
<section data-markdown><script type="text/template">
## Memory addressing restrictions
- The destination cannot be a constant (duh!)
- You cannot access memory twice in one instruction
- As the CPU does not have enough time to do so at that clock speed
- So the following instructions are invalid:
```
mov [rax], [var]
mov [rax+8], [rbx]
mov 20, [rax]
```
</script></section>
</section>
<section>
<section data-markdown id="x86insts"><script type="text/template">
# x86 Instruction Set
</script></section>
<section data-markdown><script type="text/template">
## x86 Instruction Set
- Data movement instructions
- Arithmetic instructions
- Logical instructions
- Control instructions
</script></section>
<section data-markdown><script type="text/template">
## Data Movement Instructions
- `mov` (or `movq` if you are moving 8 bytes)
- We've seen this in detail already
- `push`
- First decrements register RSP (stack pointer) by 8 (stack grows ***down***)
- push (mov) operand onto stack (8 bytes)
- sometimes you will see this as pushq (push quad word onto stack)
```
push rax
push [var]
```
</script></section>
<section data-markdown><script type="text/template">
## Data Movement Instructions
- `pop`
- Pop top element of stack to memory or register, then increment stack pointer (RSP) by 8
- Value is written to the parameter
```
pop rax
pop [var]
```
- `lea`
- Load effective address
- Place address of second parameter into the first parameter
```
lea rax, [var]
lea rdi, [rbx+4*rsi]
```
</script></section>
<section data-markdown><script type="text/template">
## Arithmetic Instructions
- `add`, `sub`
```
add <reg>, <reg>
add <reg>, <mem>
add <mem>, <reg>
add <reg>, <constant>
add <mem>, <constant>
```
- Adds (or subtracts), storing result in first operand
- Similar restrictions as with data movement instructions:
- Destination cannot be a constant
- Memory cannot be accessed twice
</script></section>
<section data-markdown><script type="text/template">
## Arithmetic Instructions
- `inc`, `dec` (increment and decrement by one)
```
inc <reg>
inc <mem>
```
- Specific examples:
```
dec rax
inc [var]
```
- `imul`
```
imul <reg64>, <reg64> (or <mem>)
imul <reg64>, <reg64> (or <mem>), <con>
```
- `idiv`
- Divide 128-bit integer in RDX:RAX by operand
```
idiv rbx
```
</script></section>
<section data-markdown><script type="text/template">
## Logical Instructions
- `and`, `or`, `xor`
```
and <reg>, <reg>
and <reg>, <mem>
and <mem>, <reg>
```
- Specific examples:
```
and rax, 0fH
xor rcx, rcx
```
</script></section>
<section data-markdown><script type="text/template">
## Control Instructions, part 1
- `jmp <label>`
- go to instruction address specified by label
- `cmp`
- This must be done prior to an *conditional* jump
- cmp operand1, operand2
- Operand1 can be a register or memory (variable)
- Operand2 can be a register, memory (variable), or a constant
- Recall that you can't access memory twice!
- Sets the *machine status word*
</script></section>
<section data-markdown><script type="text/template">
## Control Instructions, part 2
- Conditional jumps: j*condition*
- Uses the machine status word, which was set via `cmp`
- Which holds info about the results of the last instruction
- There are many 'condition's to determine whether to jump
- Example: `je <label>`
- Jump when condition code equal is set
- Others: `jne`, `jz`, `jg`, `jge`, `jl`, `jle`, `js`, etc.
</script></section>
<section data-markdown><script type="text/template">
## Control Instructions, part 3
- `call <label>`
- Subroutine call
- Pushes address of the *next* instruction onto the stack, then unconditionally jumps to the label
- `ret`
- Subroutine return
- Pops the return address from the stack, then jumps to that address
</script></section>
<section>
<h2>A code block in both C/C++ and Assembly</h2>
<table class="transparent">
<tr><td><p>C/C++ code:</p><pre><code>int n = 5;
int i = 1;
int sum = 0;
while (i <= n) {
sum += i;
i++;
}</code></pre></td>
<td style="width:50px"></td>
<td><p>Assembly code:</p><pre><code>section .data
n DQ 5
i DQ 1
sum DQ 0
section .text
loop: mov rcx, [i]
cmp rcx, [n]
jg endOfLoop
add [sum], rcx
inc qword [i]
jmp loop
endOfLoop:
</code></pre></td></tr></table>
</section>
</section>
<section>
<section id="callingconv" data-markdown><script type="text/template">
# Calling Conventions
</script></section>
<section data-markdown><script type="text/template">
## Calling of a subroutine
```
int max(int x, int y) {
int theMax = (x > y) ? x : y;
return theMax;
}
int main() {
int a = 5, b = 6;
int maxVal = max(a,b);
cout << "Max value: " << maxVal << endl;
return 0;
}
```
</script></section>
<section data-markdown><script type="text/template">
## Calling Conventions
- What is a calling convention?
- A set of rules/expectations between functions
- How (and where) are *parameters* passed?
- Which *registers* does the calling function expect to be preserved?
- Where should *local variables* be stored?
- How/where should results be *returned* from functions?
- Why?
- Separate programmers can:
- Share code more easily
- Develop libraries
</script></section>
<section data-markdown><script type="text/template">
## C Calling Convention
- Why C's calling convention?
- It's important
- Is used with both C and C++ code
- Can enable calling C library functions from assembly code
- Or other languages, too
</script></section>
<section data-markdown><script type="text/template">
## C Calling Convention
- Uses hardware stack (memory)
- Stack *grows down*, toward the lower memory addresses
- x86 instructions used for calling convention
- `pop`
- `push`
- `call`
- `ret`
- Using a stack for calling convention is implemented on most processors. Why?
- Recursion
</script></section>
<section data-markdown><script type="text/template">
## C Calling Convention Overview
- Answers to questions
- Parameters: passed in registers
- If more than 6, then params 7-*n* placed on stack
- If passing a large object, then placed on stack (doesn't fit in 64-bit register)
- Registers: saved on the stack
- Local variables: placed in memory on the stack
- Or in registers if room available
- Return value: rax register
</script></section>
<section data-markdown><script type="text/template">
## Calling Convention Overview
- Two sets of rules
- Caller: the function which calls another function
- Callee: the function which is called by another function
</script></section>
<section data-markdown><script type="text/template">
## Caller vs. Callee
```
void foo() {
// some function code ...
}
int main() {
foo();
return 0;
}
```
- `main()` is the *caller*
- `foo()` is the *callee*
</script></section>
<section data-markdown><script type="text/template">
## Register usage
- One register is used for the return value: rax
- Six registers are used for parameter passing:
- rdi, rsi, rdx, rcx, r8, r9
- Two registers may be modified by the callee: r10 and r11
- If the caller wants to keep those values, they need to be saved by pushing them onto the stack
- Actually, these two have specific uses depending on the language, so are often not used for this reason.
- Six registers may not be modified by a subroutine callee:
- rbx, rbp, r12-r15
- If it wants to use them, the subroutine must back them up (onto the stack) and restore them later
- rsp should almost never be modified directly, as it points to the top of the stack
</script></section>
<section data-markdown><script type="text/template">
## Varying number of parameters
- Three ways to have a variable number of parameters:
- Method overloading
- `Foo::bar(int)` and `Foo::bar(int, float)`
- Default parameters
- `Foo::bar (int x = 3)`
- Variable arguments
- As seen next...
</script></section>
<section data-markdown><script type="text/template">
## Variable number of arguments in C/C++
This code adapted from [here](http://www.cprogramming.com/tutorial/lesson17.html)
```
#include <cstdarg>
#include <iostream>
using namespace std;
double average (int num, ...) {
va_list arguments;
double sum = 0;
va_start (arguments, num);
for ( int x = 0; x < num; x++ )
sum += va_arg (arguments, double);
va_end (arguments);
return sum / num;
}
int main() {
cout << average(3, 12.2, 22.3, 4.5) << endl;
cout << average(5, 3.3, 2.2, 1.1, 5.5, 3.3) << endl;
}
```
</script></section>
<section data-markdown><script type="text/template">
## Dissection of the average() function
- Create a data structure to hold the list of arguments:
```
va_list arguments;
```
- Initialize the arguments to store all values after num:
```
va_start ( arguments, num );
```
- Loop until all numbers are added:
```
for ( int x = 0; x < num; x++ )
```
- Adds the next value in argument list to sum:
```
sum += va_arg ( arguments, double );
```
- Clean up the list:
```
va_end ( arguments );
```
- Return the average:
```
return sum / num;
```
</script></section>
<section data-markdown><script type="text/template">
## Example: Output in C
The C equivalent of cout is a function called `printf`
```
printf ("A %s, a %s, a %s: %s!\n", "man",
"plan", "canal", "Panama");
printf ("A percent sign: %%\n");
printf ("An int: %d\n", i);
printf ("A float with 2 decimal digits: %.2f\n",
float_value);
```
Output:
```
A man, a plan, a canal: Panama!
A percent sign: %
An int: 3
A float with 2 decimal digits: 3.14
```
</script></section>
</section>
<section>
<section id="caller" data-markdown><script type="text/template">
# Caller Rules
</script></section>
<section data-markdown><script type="text/template">
## Caller Summary
- Prologue
- Tasks to take care of BEFORE calling a subroutine
- Call the subroutine with the `call` opcode
- Epilogue
- Tasks to complete AFTER subroutine call returns
- (It is not really called this, but I use this to parallel the equivalent components in the callee convention)
</script></section>
<section data-markdown><script type="text/template">
## Callee Summary
- Prologue
- Tasks to perform BEFORE executing the function body
- Function body
- Epilogue
- Tasks to perform AFTER executing function body, but BEFORE leaving function
</script></section>
<section data-markdown><script type="text/template">
## Caller Rules/Responsibilities
- *Before* calling the function (the prologue)
- Save registers that might be needed after the call (r10, r11, or param registers if applicable)
- Place parameters in registers / on stack
- rdi, rsi, rdx, rcx, r8, r9
- Then push extra params onto stack
- *Call* the function
- `call` instruction places return address on stack
- *After* the called function returns (the epilogue)
- Remove parameters from stack (if applicable)
- Restore saved registers (if applicable)
</script></section>
<section data-markdown><script type="text/template">
## Caller Rules ("Prologue")
1. Caller-saved registers
- Registers which the caller must save (push onto the stack) ONLY if it wants the values preserved.
- r10, r11, and registers used for parameters if need value saved
2. Parameters
- First six are passed in through registers (see previous slide)
- params 7-n pushed in *reverse order* (last parameter first) onto stack
3. Call the subroutine
- Use the `call` instruction
- pushes the return address onto the stack and branches to the subroutine
</script></section>
<section data-markdown><script type="text/template">
## Caller Rules ("Epilogue")
1. Remove parameters
- Parameters pushed onto stack must be removed
- Restore stack to the state before the call
- What is done with the parameters?
2. Return value
- If any, held in rax
3. Restore caller-saved registers
- `pop` them off the stack (Caller can assume no other registers were modified)
</script></section>
<section data-markdown><script type="text/template">
## Caller Rules Example
We'll see this code in the following slides:
```
long myFunc(long a, long b, long c) {
long result;
// some code
return result;
}
int main() {
long x = 1, z = 3;
long retVal = myFunc(x, 123, z);
//...
return 0;
}
```
</script></section>
<section data-markdown><script type="text/template">
## Caller Rules Example
```
; Want to call a function "myFunc" that takes three
; integer parameters. First parameter is in rax.
; Second parameter is the constant 123. Third
; parameter is in memory location "var"
push rdi ; rdi will be a param, so saving it
; long retVal = myFunc(x, 123, z);
mov rdi, rax ; put first param in rdi
mov rsi, 123 ; put second param in rsi
mov rdx, [var] ; put third param in rdx
call myFunc ; call the function
pop rdi ; restore saved rdi value
; return value of myFunc is now available in rax
; (if there is any return value)
```
</script></section>
<section>
<h2>Stack Memory Visualization for myFunc</h2>
<p>This is just before the <code>call</code> opcode is invoked.</p>
<hr>
<table class="transparent">
<tr><td>↑</td><td style="width:50px"></td><td class="border" style="width:300px">value of rdi</td><td style="width:25px"></td><td>← rsp</td></tr>
<tr><td>To higher addresses</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>(to 0xffffffff)</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>To lower addresses</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>(to 0x00000000)</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>↓</td><td style="width:50px"></td><td class="border" style="width:300px;border-bottom:medium solid;"> </td><td style="width:25px"></td><td> </td></tr>
</table>
</section>
<section data-transition="fade" data-transition-speed="fast">
<h2>Stack Memory Visualization for myFunc</h2>
<p>This is just <i><b>after</b></i> the <code>call</code> opcode is invoked.</p>
<hr>
<table class="transparent">
<tr><td>↑</td><td style="width:50px"></td><td class="border" style="width:300px">value of rdi</td><td style="width:25px"></td><td> </td></tr>
<tr><td>To higher addresses</td><td style="width:50px"></td><td class="border" style="width:300px">return address</td><td style="width:25px"></td><td>← rsp</td></tr>
<tr><td>(to 0xffffffff)</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>To lower addresses</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>(to 0x00000000)</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>↓</td><td style="width:50px"></td><td class="border" style="width:300px;border-bottom:medium solid;"> </td><td style="width:25px"></td><td> </td></tr>
</table>
</section>
</section>
<section>
<section id="callee" data-markdown><script type="text/template">
# Callee Rules
</script></section>
<section data-markdown><script type="text/template">
## Callee Summary (again)
- Prologue
- Tasks to perform BEFORE executing the function body
- Function body
- Epilogue
- Tasks to perform AFTER executing function body, but BEFORE leaving function
</script></section>
<section data-markdown><script type="text/template">
## Caller Rules Example
We will see this code in the following slides:
```
long myFunc(long a, long b, long c) {
long result;
// some code
return result;
}
int main() {
long x = 1, z = 3;
long retVal = myFunc(x, 123, z);
//...
return 0;
}
```
</script></section>
<section data-markdown><script type="text/template">
## Callee Rules (Prologue)
Before the body of the function:
1. Allocate local variables
- Make space on stack (decrement stack pointer)
```
sub rsp, 8
```
That is it. NOTE: caller is also allowed to use valid registers for local variables, so sometimes you will see that instead.
</script></section>
<section data-markdown id="pushrbp"><script type="text/template">
## PUBLIC SERVICE ANNOUNCEMENT!
- Note that you might see the following:
```
push rbp ; at the start of the callee
mov rbp, rsp
...
pop rbp ; just before the ending 'ret'
```
- This is a 32-bit x86 convention that is no longer officially used, but is sometimes seen anyway.
- To omit it, add the `-fomit-frame-pointer` flag to the compilation line
</script></section>
<section>
<h2>Callee Rules (Prologue)</h2>
<ol start="3">
<li>Save callee-save registers<ul>
<li>rbx, rbp, r12-r15</li>
<li>only need to do this if callee intends to use them, otherwise, no need to save their contents</li>
</ul></li></ol>
<p> </p>
<p>THEN, perform body of the function</p>
</section>
<section data-markdown><script type="text/template">
## Callee Rules (Epilogue)
1. Return value saved to rax
2. Restore callee-saved registers
- `pop` from stack (in reverse order from which pushed)
3. Deallocate local variables
```
add rsp, 8 //constant here depends on size of locals
```
5. Return
```
ret
```
</script></section>
<section data-markdown><script type="text/template">
## Callee Rules Example
With a bit more code in the `myFunc()` body (NOTE the use of long here because they are 64-bit):
```
long myFunc(long a, long b, long c) {
long result;
result = c;
result += b;
return result;
}
int main() {
long x = 1, z = 3;
long retVal = myFunc(x, 123, z);
//...
return 0;
}
```
</script></section>
<section data-markdown><script type="text/template">
## Callee Rules Example (1)
```
section .text
myFunc:
; prologue
sub rsp, 8 ; room for a 64-bit local var (result)
push rbx ; save callee-save registers
push rbp ; both will be used by myFunc
```
</script></section>
<section data-markdown><script type="text/template">
## Callee Rules Example (2)
```
; subroutine body
mov rax, rdi ; param 1 to rax
mov rbp, rsi ; param 2 to rbp
mov rbx, rdx ; param 3 to rbx
mov [rsp+16], rbx ; put rbx into local var
add [rsp+16], rbp ; add rbp into local var
mov rax, [rsp+16] ; mov contents of local var to rax
; (return value/final result)
```
</script></section>
<section data-markdown><script type="text/template">
## Callee Rules Example (3)
```
; subroutine epilogue
pop rbp ; recover callee save registers
pop rbx ; REVERSE of when pushed
add rsp, 8 ; deallocate local var(s)
ret ; pop top value from stack, jump there
```
</script></section>
<section>
<h2>Stack Memory Visualization for myFunc</h2>
<p>This is just <i>after</i> the caller invokes the <code>call</code> opcode.</p>
<hr>
<table class="transparent">
<tr><td>↑</td><td style="width:50px"></td><td class="border" style="width:300px">value of rdi</td><td style="width:25px"></td><td> </td></tr>
<tr><td>To higher addresses</td><td style="width:50px"></td><td class="border" style="width:300px">return address</td><td style="width:25px"></td><td>← rsp</td></tr>
<tr><td>(to 0xffffffff)</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>To lower addresses</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>(to 0x00000000)</td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td>↓</td><td style="width:50px"></td><td class="border" style="width:300px;border-bottom:medium solid;"> </td><td style="width:25px"></td><td> </td></tr>
</table>
</section>
<section data-transition="fade" data-transition-speed="fast">
<h2>Stack Memory Visualization for myFunc</h2>
<p>This is just after the callee invokes the <code>sub rsp, 8</code> opcode.</p>
<hr>
<table class="transparent">
<tr><td>↑</td><td style="width:50px"></td><td class="border" style="width:300px">value of rdi</td><td style="width:25px"></td><td> </td></tr>
<tr><td>To higher addresses</td><td style="width:50px"></td><td class="border" style="width:300px">return address</td><td style="width:25px"></td><td> </td></tr>
<tr><td>(to 0xffffffff)</td><td style="width:50px"></td><td class="border" style="width:300px">local var (result)</td><td style="width:25px"></td><td>← rsp</td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>
<tr><td></td><td style="width:50px"></td><td class="border" style="width:300px"> </td><td style="width:25px"></td><td> </td></tr>