External Data and Text Flashcards

1
Q

In the C language, local (automatic) variables are always allocated in the stack frame of a function

What is its scope and lifetime?

A

Scope: local to block of code where declared

Lifetime: life of the block of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the scope and lifetime of static local variables?

A

Scope: block of code where declared

Lifetime: life of the program (persist from call to call of the function)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Stack frames can’t be stored on the stack frame for the function. Why?

A

Stack memory may be used by other functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where are static local variables stored?

A

In a separate section of RAM

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the ret instruction do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What do the stp instructions do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The FP and the stored FP values in the frame records form a ___________

A

linked list

[diagram in notes]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

x19-x28

callee-saved register

The function must restore the data in these registers just before it returns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which other registers can the callee use?

A

x9-x15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

By convention, these registers are not saved/restored by the called function

Thus .. ?

A

Are only safe to use in calling code between function calls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The calling code can save these registers to the stack, if it is necessary to preserve their value over a function call

Are..

A

Caller-saved register

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How many arguments can be passed into a function using register x0 - x7?

A

8 or fewer
ints, short intsm and chars use w0 - w7

long ints use x0 - x7
[Example in notes]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The subroutine is free to _______ registers x0 - x7 as it executes

A

overwrite

The register contents are not preserved over a function call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Global Variables
Scope
Lifetime
Where they are stored

A

Scope: global (from declaration onwards)
Lifetime: life of program
Are stored in a separate section of RAM

[example in notes]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Static Global Variables
Scope
Lifetime
Where they are stored

A

Scope: local to file (from declaration onwards)
Lifetime: life of a program
Are stored in a separate section of RAM

[example in notes]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Program may allocate _ sections of memory:

What do they contain and which memory are they?

A
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

17
Q

These sections are located in _________, just after the section reserved for the OS kernel

A

low memory

[diagram in notes]

18
Q

Pseudo-ops are used to indicate …

Give examples

A

… what follows goes into a particular section

.text (is the default section when assembling)
.data
.bss

19
Q

The assembler uses a ____________ for each section

A

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

20
Q

What happens when the OS loads the program into RAM?

A

The text and data sections are loaded first

The bss section is then zeroed

21
Q

What are external variables?

A

Are non-local variables, allocated in the data or bss sections

Are used to implement C language global and static local variables

22
Q

How can external variables be allocated and initialized?

A
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]

23
Q

What do the labels represent?

A

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]

24
Q

How can uninitialized space be allocated?

A

With the .skip pseduo-op

25
Q

Which pseudo-op should be used to make a variable be made available to other compilation units?

A

.global

26
Q

Which pseudo-op does the bss section usually only use?

A

.skip
All bss memory is zeroed before program execution
Initializing memory to non-zero values (with .word, .hword, etc.) doesn’t make sense

27
Q

Which section are programmer-initialized constants put into?

A

The text section

The must be before or between functions

28
Q

What does ASCII stand for and what does it do?

A

American Standard Code for Information Interchange

It encodes characters using 7 bits, stored in a byte

29
Q

How can character constants be encoded in assembly?

A
  • The hex code
  • The character in single quotes
    Note: may interfere with m4
30
Q

What is a string and how can it be initialized?

A

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

31
Q

In C, strings are null terminated.. How is this done?

A

Could be done using two pseudo-ops

But more conveniently with .asciz or .string

32
Q

What is a string literal?

A

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

33
Q

How are external Arrays of Pointers created?

A

Using a list of labels

[example in notes]

34
Q

What are command-line arguments used for?

A

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]