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
Which pseudo-op should be used to make a variable be made available to other compilation units?
.global
Which pseudo-op does the bss section usually only use?
.skip
All bss memory is zeroed before program execution
Initializing memory to non-zero values (with .word, .hword, etc.) doesn’t make sense
Which section are programmer-initialized constants put into?
The text section
The must be before or between functions
What does ASCII stand for and what does it do?
American Standard Code for Information Interchange
It encodes characters using 7 bits, stored in a byte
How can character constants be encoded in assembly?
- The hex code
- The character in single quotes
Note: may interfere with m4
What is a string and how can it be initialized?
A string is an array of characters
Could be initialized in memory one byte at a time
But the .ascii pseudo-op is more convenient
In C, strings are null terminated.. How is this done?
Could be done using two pseudo-ops
But more conveniently with .asciz or .string
What is a string literal?
A string literal is a read-only array of characters, allocated in the text section
The literal usually has a label, which represents the address of the first character in the array
The address can be passed as a pointer argument into a function using an x register
How are external Arrays of Pointers created?
Using a list of labels
[example in notes]
What are command-line arguments used for?
They allow you to pass values from the shell into your program
In C: main (int argc, char argv[])
- argc is the number of arguments
- argv[] is an array of pointers to the arguments (represented as strings)
[example in notes]