External Data and Text Flashcards
In the C language, local (automatic) variables are always allocated in the stack frame of a function
What is its scope and lifetime?
Scope: local to block of code where declared
Lifetime: life of the block of code
What is the scope and lifetime of static local variables?
Scope: block of code where declared
Lifetime: life of the program (persist from call to call of the function)
Stack frames can’t be stored on the stack frame for the function. Why?
Stack memory may be used by other functions
Where are static local variables stored?
In a separate section of RAM
What does the ret instruction do?
It returns from a subroutine back to the calling code
It transfers control to the address stored in the link register (x30)
It jumps to the instruction immediately following the original bl in calling code
What do the stp instructions do?
They create a frame record in each function’s stack frame
It safely stores the LR (x30), in case it is changed by a bl in the body of the function
It is restored by the ldp instruction, just before the ret
The FP and the stored FP values in the frame records form a ___________
linked list
[diagram in notes]
A called function must save/restore the state of the calling code
If it uses any of the registers __________ it must save their data to the stack at the beginning of the function
Are _________________
What must the function do in these registers just before it returns
x19-x28
callee-saved register
The function must restore the data in these registers just before it returns
Which other registers can the callee use?
x9-x15
By convention, these registers are not saved/restored by the called function
Thus .. ?
Are only safe to use in calling code between function calls
The calling code can save these registers to the stack, if it is necessary to preserve their value over a function call
Are..
Caller-saved register
How many arguments can be passed into a function using register x0 - x7?
8 or fewer
ints, short intsm and chars use w0 - w7
long ints use x0 - x7
[Example in notes]
The subroutine is free to _______ registers x0 - x7 as it executes
overwrite
The register contents are not preserved over a function call
Global Variables
Scope
Lifetime
Where they are stored
Scope: global (from declaration onwards)
Lifetime: life of program
Are stored in a separate section of RAM
[example in notes]
Static Global Variables
Scope
Lifetime
Where they are stored
Scope: local to file (from declaration onwards)
Lifetime: life of a program
Are stored in a separate section of RAM
[example in notes]
Program may allocate _ sections of memory:
What do they contain and which memory are they?
3 text Contains: Program text (machine code) Read-only, programmer-initialized data Is read-only memory Attempts to write to this memory causes segmentation fault
data
Contains:
Programmer-initialized data
Is read/write memory
bss
Contains:
zero-initialized data
Is read/write memory
These sections are located in _________, just after the section reserved for the OS kernel
low memory
[diagram in notes]
Pseudo-ops are used to indicate …
Give examples
… what follows goes into a particular section
.text (is the default section when assembling)
.data
.bss
The assembler uses a ____________ for each section
location counter
Starts at 0, and increases as instructions and data are processed
The final step of assembly gathers all code and data into the appropriate sections
What happens when the OS loads the program into RAM?
The text and data sections are loaded first
The bss section is then zeroed
What are external variables?
Are non-local variables, allocated in the data or bss sections
Are used to implement C language global and static local variables
How can external variables be allocated and initialized?
Using the pseudo-ops: .dword (8 bytes) .word (4 bytes) .hword (2 bytes) .byte (1 byte) General form: label: pseudo-op value1,[value 2, ...]
[Example in notes]
What do the labels represent?
64-bit addresses
Use adrp and add to put the address into a register
They use ldr or str to access the variable
[Example in notes]
How can uninitialized space be allocated?
With the .skip pseduo-op