From ee6d3d8493939fb732bcf53c92b2edc10a7fa31c Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Mon, 18 Nov 2024 11:01:18 -0700 Subject: [PATCH] Notes on frame pointers. --- .../plugins/obsidian-to-anki-plugin/data.json | 17 +- notes/_journal/2024-10/2024-10-09.md | 2 +- notes/_journal/2024-11-18.md | 4 +- notes/c17/types/derived.md | 269 +++++++++++------- notes/x86-64/instructions/access.md | 28 +- notes/x86-64/instructions/arithmetic.md | 4 +- notes/x86-64/instructions/logical.md | 8 +- notes/x86-64/instructions/procedures.md | 169 +++++++++++ notes/x86-64/procedures.md | 252 +++++++++------- notes/x86-64/registers.md | 46 +-- 10 files changed, 521 insertions(+), 278 deletions(-) create mode 100644 notes/x86-64/instructions/procedures.md diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index 0be9e49..b2fd3fa 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -526,7 +526,7 @@ "set/axioms.md": "063955bf19c703e9ad23be2aee4f1ab7", "_journal/2024-05-14.md": "f6ece1d6c178d57875786f87345343c5", "_journal/2024-05/2024-05-13.md": "d549dd75fb42b4280d4914781edb0113", - "x86-64/registers.md": "a1daead80c55434c0aed939c53103f9d", + "x86-64/registers.md": "5fc70aaebf9dc7d4b04c72650bedacfe", "_journal/2024-05-15.md": "4e6a7e6df32e93f0d8a56bc76613d908", "_journal/2024-05/2024-05-14.md": "f6ece1d6c178d57875786f87345343c5", "_journal/2024-05-16.md": "580c7ec61ec56be92fa8d6affcf0a5f6", @@ -593,9 +593,9 @@ "lambda-calculus/alpha-conversion.md": "6df655e60976715e5c6fbbe72b628c6d", "lambda-calculus/index.md": "0e47dcd2ac0c84ad4c1a5e7609dade5f", "x86-64/instructions/condition-codes.md": "9c05ed99f5c96162e25f0ec4db55c656", - "x86-64/instructions/logical.md": "49d40018f1fcb4ed1595d9175bbaab57", - "x86-64/instructions/arithmetic.md": "df57fdc8d260d0812464b08565ef4c51", - "x86-64/instructions/access.md": "4589d79f9915ffeeab7b75d2208b7917", + "x86-64/instructions/logical.md": "a15c7da43cb97badef8ba4f8aadf9cbb", + "x86-64/instructions/arithmetic.md": "e2c4c9caa51e089e313d6c9d3c3c0a12", + "x86-64/instructions/access.md": "e5a282c053019fc63df5114b1c052c2f", "x86-64/instructions/index.md": "cc5505d01d8409255fa42e87bfe15f07", "_journal/2024-06-09.md": "935b3ddf65c51e680ac5c000c7e380af", "_journal/2024-06/2024-06-08.md": "9e1ebc8882a395b96ca765ad5c982d68", @@ -798,7 +798,7 @@ "c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875", "c17/types/simple.md": "44d56a2998f3bba578f14672c8b0ee3c", "c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d", - "c17/types/derived.md": "7e5d0939f72ee02ae00a78a2af17a0fd", + "c17/types/derived.md": "26bcaee99aba7720fd76d92e10770933", "c17/types/basic.md": "5064e21e683c0218890058882e06b6f3", "c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b", "_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2", @@ -879,9 +879,9 @@ "_journal/2024-10/2024-10-06.md": "65a8e538144f59ee9e7296110cc9aa14", "_journal/2024-10-09.md": "66acb6c8abbf0c860966223f3299c2bc", "_journal/2024-10/2024-10-08.md": "2f737d8198b12bf635808964c4887ae1", - "x86-64/procedures.md": "3464bb6229f85973961c6693f47d6f20", + "x86-64/procedures.md": "4f8bb79e11b710eaccaf7ecb847bd69f", "_journal/2024-10-10.md": "29e4be2c164c63f8b7aab56bddee3dd6", - "_journal/2024-10/2024-10-09.md": "b718c6f4cfe3069fcc0faf07ec414f73", + "_journal/2024-10/2024-10-09.md": "458811cdab772a7b3dbc1dd8ae5c51ee", "_journal/2024-10-15.md": "6e696a8d16ba257ed89e8564b771d290", "_journal/2024-10/2024-10-14.md": "fbb5a82a18e3acca55bb46a824a9419d", "_journal/2024-10/2024-10-13.md": "8882c01060a4a909fd890180db356e73", @@ -940,7 +940,8 @@ "_journal/2024-11/2024-11-13.md": "db4f2e5db5bb1d2199fee88eb1a6c881", "_journal/2024-11/2024-11-12.md": "24c858a93c0a5063820bd96207e6f8ab", "_journal/2024-11/2024-11-11.md": "d3ad8ddc5ecdb2cc08773fdef2ce6869", - "_journal/2024-11/2024-11-10.md": "38ce405754e3823c8239df44546f3b35" + "_journal/2024-11/2024-11-10.md": "38ce405754e3823c8239df44546f3b35", + "x86-64/instructions/procedures.md": "fd83b80e35454d4bca12c4cf8127cf91" }, "fields_dict": { "Basic": [ diff --git a/notes/_journal/2024-10/2024-10-09.md b/notes/_journal/2024-10/2024-10-09.md index d2f9552..668b9d9 100644 --- a/notes/_journal/2024-10/2024-10-09.md +++ b/notes/_journal/2024-10/2024-10-09.md @@ -8,4 +8,4 @@ title: "2024-10-09" - [ ] Sheet Music (10 min.) - [ ] Korean (Read 1 Story) -* Start adding notes on [[procedures]]. \ No newline at end of file +* Start adding notes on [[x86-64/procedures]]. \ No newline at end of file diff --git a/notes/_journal/2024-11-18.md b/notes/_journal/2024-11-18.md index cde020a..4abcfb8 100644 --- a/notes/_journal/2024-11-18.md +++ b/notes/_journal/2024-11-18.md @@ -6,4 +6,6 @@ title: "2024-11-18" - [x] KoL - [ ] OGS - [ ] Sheet Music (10 min.) -- [ ] Korean (Read 1 Story) \ No newline at end of file +- [ ] Korean (Read 1 Story) + +* Notes on [[x86-64/procedures#Frame Pointer|frame pointers]]. \ No newline at end of file diff --git a/notes/c17/types/derived.md b/notes/c17/types/derived.md index b8d0983..f0bf625 100644 --- a/notes/c17/types/derived.md +++ b/notes/c17/types/derived.md @@ -40,7 +40,7 @@ END%% ### Arrays -An array is a contiguous sequence of objects. An array is either a **fixed-length array** (FLA) or a **variable-length array** (VLA). +An array is a contiguous sequence of objects. An array is either a **fixed-length array** or a **variable-length array**. %%ANKI Cloze @@ -155,114 +155,6 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co END%% -%%ANKI -Basic -Where *must* a VLA be declared? -Back: Within a function. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What two ways can the length of an FLA be specified? -Back: By an ICE or an initializer. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -double a[4]; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -double a[]; -``` -Back: N/A. This is invalid syntax. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -double a[] = { 0 }; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -int n = 2; -double a[n]; -``` -Back: VLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -#define n 2 -double a[n]; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -enum { n = 2 }; -double a[n]; -``` -Back: FLA. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is the following an FLA or VLA? -```c -int n = 2; -double a[n] = {0}; -``` -Back: N/A. This is an invalid assignment. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -For the following to be an FLA, what must `N` be? -```c -double a[N]; -``` -Back: An ICE. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - %%ANKI Basic Replace `EXPR` in the following with an expression to compute the length of `A`. @@ -555,6 +447,165 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co END%% +%%ANKI +Basic +Is the following an FLA or VLA? +```c +double a[4]; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +double a[]; +``` +Back: N/A. This is invalid syntax. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +double a[] = { 0 }; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +int n = 2; +double a[n]; +``` +Back: VLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +#define n 2 +double a[n]; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +enum { n = 2 }; +double a[n]; +``` +Back: FLA. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is the following an FLA or VLA? +```c +int n = 2; +double a[n] = {0}; +``` +Back: N/A. This is an invalid assignment. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +#### Fixed-Length + +A fixed-length array (FLA) has a predetermined size. Their stack allocations can be computed at compilation time. + +%%ANKI +Basic +What is FLA an acronym for? +Back: **F**ixed-**l**ength **a**rray. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What two ways can the length of an FLA be specified? +Back: By an ICE or an initializer. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +For the following to be an FLA, what must `N` be? +```c +double a[N]; +``` +Back: An ICE. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* must FLAs be declared within a function? +Back: N/A. They can exist elsewhere. +Tags: x86-64 +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +#### Variable-Length + +A variable-length array (VLA) has its size determined at runtime. Their stack allocations must be determined with respect to other registers available to the frame. + +%%ANKI +Basic +What is VLA an acronym for? +Back: **V**ariable-**l**ength **a**rray. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Where *must* a VLA be declared? +Back: Within a function. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* do variable-length arrays require use of a frame pointer? +Back: Offsets depend on how much space must be allocated on the stack. +Tags: x86-64 +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +*Why* must VLAs be declared within a function? +Back: Their implementation relies on frame pointers. +Tags: x86-64 +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + ### Structures A `struct` is a grouping of data together. It has the following general form: diff --git a/notes/x86-64/instructions/access.md b/notes/x86-64/instructions/access.md index 48514aa..a49380b 100644 --- a/notes/x86-64/instructions/access.md +++ b/notes/x86-64/instructions/access.md @@ -320,10 +320,7 @@ END%% %%ANKI Basic -What equivalent no-operand instruction does x86-64 provide? -```asm -movslq %eax, %rax -``` +How is `movslq %eax, %rax` equivalently written using a single no-operand instruction? Back: `cltq` Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. @@ -331,10 +328,7 @@ END%% %%ANKI Basic -What equivalent no-operand instruction does x86-64 provide? -```asm -movzlq %eax, %rax -``` +How is `movzlq %eax, %rax` equivalently written using a single no-operand instruction? Back: N/A. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. @@ -396,8 +390,8 @@ Basic How is `pushq %rbp` equivalently written using a pair of instructions? Back: ```asm -subq $8,%rsp -movq %rbp,(%rsp) +subq $8, %rsp +movq %rbp, (%rsp) ``` Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. @@ -408,8 +402,8 @@ Basic How is `popq %rax` equivalently written using a pair of instructions? Back: ```asm -movq (%rsp),%rax -addq $8,%rsp +movq (%rsp), %rax +addq $8, %rsp ``` Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. @@ -448,8 +442,12 @@ END%% %%ANKI Basic -What is the `%rsp` register typically used for? -Back: The stack pointer. +Why is register `%rsp` named the way it is? +Back: It stands for **s**tack **p**ointer. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -END%% \ No newline at end of file +END%% + +## Bibliography + +* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. \ No newline at end of file diff --git a/notes/x86-64/instructions/arithmetic.md b/notes/x86-64/instructions/arithmetic.md index a3298ae..a30dde0 100644 --- a/notes/x86-64/instructions/arithmetic.md +++ b/notes/x86-64/instructions/arithmetic.md @@ -6,7 +6,7 @@ tags: - x86-64 --- -### Load Effective Address +### LEAQ | Instruction | Operands | Effect | Description | | ----------- | -------- | ------- | ---------------------- | @@ -237,7 +237,7 @@ END%% %%ANKI Basic -Which `SUB` instruction is equivalent to `decq %rcx`? +How is `decq %rcx` equivalently written using the `SUB` instruction class? Back: ```asm subq $1, %rcx diff --git a/notes/x86-64/instructions/logical.md b/notes/x86-64/instructions/logical.md index 82f8b06..f82af86 100644 --- a/notes/x86-64/instructions/logical.md +++ b/notes/x86-64/instructions/logical.md @@ -8,10 +8,10 @@ tags: ### Unary Operations -| Instruction | Operands | Effect | Description | -| ----------- | -------- | ---------- | ----------- | -| `neg[bwlq]` | D | D <- -D | Negate | -| `not[bwlq]` | D | D <- ~D | Complement | +| Instruction | Operands | Effect | Description | +| ----------- | -------- | ------- | ----------- | +| `neg[bwlq]` | D | D <- -D | Negate | +| `not[bwlq]` | D | D <- ~D | Complement | %%ANKI Basic diff --git a/notes/x86-64/instructions/procedures.md b/notes/x86-64/instructions/procedures.md new file mode 100644 index 0000000..68ed93d --- /dev/null +++ b/notes/x86-64/instructions/procedures.md @@ -0,0 +1,169 @@ +--- +title: Procedures +TARGET DECK: Obsidian::STEM +FILE TAGS: x86-64 +tags: + - x86-64 +--- + +## Overview + +The details of procedures are detailed in [[x86-64/procedures|procedures]]. This file touches on the most relevant instruction classes. + +## CALL and RET + +Like [[conditions#JMP|JMP]] instructions, `call` allows specifying a direct or indirect operand. `call` pushes the address of the instruction following it onto the stack and updates the PC to the operand. `ret` reverts these steps. + +| Instruction | Operands | Description | +| ----------- | ----------- | ---------------- | +| `call` | Label | Procedure call | +| `call` | \**Operand* | Procedure call | +| `ret` | | Return from call | + +%%ANKI +Cloze +The {`call`} instruction is the counterpart to the {`ret`} instruction. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What two things does the `call` instruction do? +Back: It pushes the return address on the stack and updates the PC. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What two things does the `ret` instruction do? +Back: It pops the return address off the stack and updates the PC. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How is `ret` equivalently written using a line of assembly? +Back: +```asm +popq %rip +``` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +The operand forms of `call` mirror what other instruction class? +Back: `JMP` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +A `call` instruction pushes what address onto the stack? +Back: That of the instruction following the `call` instruction. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What return address is pushed onto the stack after `call` is run? +```x86 +1: ... +2: callq .L1 +3: ... +``` +Back: `3` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What address is the PC updated to after `call` is run? +```x86 +1: ... +2: callq .L1 +3: ... +``` +Back: That corresponding to label `.L1`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Which register(s) does a `call` instruction update? +Back: `%rsp` and `%rip`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Which register(s) does a `ret` instruction update? +Back: `%rsp` and `%rip`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +## LEAVE + +`leave` parallels the `ret` instruction. It is equivalent to the following two instructions: + +```asm +movq %rbp, %rsp +popq %rbp +``` + +%%ANKI +Basic +What two things does the `leave` instruction do? +Back: Restores the stack pointer and pops off the caller-saved base pointer. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How is `leave` equivalently written using a pair of instructions? +Back: +```asm +movq %rbp, %rsp +popq %rbp +``` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Fixed-sized frames use which of `leave` and/or `ret`? +Back: Just `ret`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Variable-sized frames use which of `leave` and/or `ret`? +Back: Bot +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +{1:`ret`} is to {2:`%rsp`} whereas {2:`leave`} is to {1:`%rbp`}. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +## Bibliography + +* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. \ No newline at end of file diff --git a/notes/x86-64/procedures.md b/notes/x86-64/procedures.md index 5162f4d..94134b7 100644 --- a/notes/x86-64/procedures.md +++ b/notes/x86-64/procedures.md @@ -134,100 +134,7 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% -## CALL and RET - -Like [[conditions#JMP|JMP]] instructions, `call` allows specifying a direct or indirect operand. `call` pushes the address of the instruction following it onto the stack and updates the PC to the operand. `ret` reverts these steps. - -| Instruction | Operands | Description | -| ----------- | ----------- | ---------------- | -| `call` | Label | Procedure call | -| `call` | \**Operand* | Procedure call | -| `ret` | | Return from call | - -%%ANKI -Cloze -The {`call`} instruction is the counterpart to the {`ret`} instruction. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What two things does the `call` instruction do? -Back: It pushes the return address on the stack and updates the PC. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What two things does the `ret` instruction do? -Back: It pops the return address off the stack and updates the PC. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -The operand forms of `call` mirror what other instruction class? -Back: `JMP` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -A `call` instruction pushes what address onto the stack? -Back: That of the instruction following the `call` instruction. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What return address is pushed onto the stack after `call` is run? -```x86 -1: ... -2: callq .L1 -3: ... -``` -Back: `3` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What address is the PC updated to after `call` is run? -```x86 -1: ... -2: callq .L1 -3: ... -``` -Back: That corresponding to label `.L1`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Which register(s) does a `call` instruction update? -Back: `%rsp` and `%rip`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Which register(s) does a `ret` instruction update? -Back: `%rsp` and `%rip`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -## Local Storage - -### On the Stack +## Local Variables x86-64 employs 6 registers for passing integral (i.e. integer and pointer) arguments between caller and callee. @@ -633,7 +540,7 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% -### In Registers +## Callee-Saved Registers x86-64 employs 6 registers as callee-saved registers. If procedure `P` calls procedure `Q`, `Q` *must* preserve the values of these registers. That is, if `Q` were to modify these registers, `Q` is also responsible for restoring these values before returning back to `P`. @@ -660,6 +567,14 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +%%ANKI +Basic +Which registers are designated as caller-saved? +Back: All but callee-saved registers and `%rsp`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + %%ANKI Basic *What* is a callee-saved register? @@ -691,6 +606,153 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +%%ANKI +Basic +Which number-suffixed register(s) are used for passing integral arguments to procedures? +Back: `%r8` and `%r9`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Which number-suffixed register(s) are conventionally designated "callee-saved"? +Back: `%r12`, `%13`, `%r14`, and `%r15`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Which number-suffixed register(s) are conventionally designated "caller-saved"? +Back: `%r10` and `%r11`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What does it mean for a register to be callee-saved? +Back: The callee must ensure the register's value is unchanged by the time it returns. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What does it mean for a register to be caller-saved? +Back: Any procedure can modify the register's value. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +*Why* are caller-saved registers named the way they are? +Back: It's up to the caller to preserve their value before a procedure call. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What misnomer does designating `%r10` and `%r11` as caller-saved registers introduce? +Back: This seems to imply there are no other caller-saved registers. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +## Frame Pointer + +The **frame pointer**, also known as the **base pointer**, marks the start of a variable-sized frame. `%rbp` is pushed onto the stack (since it is caller-saved) and its value updated to the new value of `%rsp`. Relative offsets are then computed according to it. + +%%ANKI +Basic +Why is register `%rbp` named the way it is? +Back: It stands for **b**ase **p**ointer. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +{1:`%rsp`} is to {2:fixed}-sized frames whereas {2:`%rbp`} is to {1:variable}-sized frames. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +When is the `%rbp` register used? +Back: When working with variable-sized frames. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +In fixed-sized frames, what is the first callee-saved register on the stack? +Back: Indeterminate. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +In variable-sized frames, what is the first callee-saved register on the stack? +Back: `%rbp`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What is typically the ultimate instruction of a fixed-sized frame? +Back: `ret` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What is typically the penultimate instruction of a fixed-sized frame? +Back: Indeterminate. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What is typically the ultimate instruction of a variable-sized frame? +Back: `ret` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What is typically the penultimate instruction of a variable-sized frame? +Back: `leave` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Assume frame `Q` starts at address `S`. If applicable, what address is its frame pointer set to? +Back: `S + 8` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Assume frame `Q` starts at address `S`. When applicable, *why* is `%rbp` set to `S + 8`? +Back: It's the value of `%rsp` after pushing the previous value of `%rbp`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + ## Bibliography * Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. diff --git a/notes/x86-64/registers.md b/notes/x86-64/registers.md index 8583606..8eb1fc3 100644 --- a/notes/x86-64/registers.md +++ b/notes/x86-64/registers.md @@ -309,50 +309,10 @@ END%% %%ANKI Basic -Which number-suffixed register(s) are used for passing integral arguments to procedures? -Back: `%r8` and `%r9`. +What misnomer does designating registers for a certain purpose introduce? +Back: Designations are convention-based. Registers can be used for any purpose. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Which number-suffixed register(s) are conventionally designated "callee-saved"? -Back: `%r12`, `%13`, `%r14`, and `%r15`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Which number-suffixed register(s) are conventionally designated "caller-saved"? -Back: `%r10` and `%r11`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What does it mean for a register to be callee-saved? -Back: The callee must ensure the register's value is unchanged by the time it returns. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What does it mean for a register to be caller-saved? -Back: Any procedure can modify the register's value. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -*Why* are caller-saved registers named the way they are? -Back: It's up to the caller to preserve their value before a procedure call. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - + END%% ## Condition Codes