Compilation etc part 2 Flashcards

Suroutines & Stacks

1
Q

What is a subroutine?

A

A subroutine is a set of instructions designed to perform a frequently used operation, packaged as a unit. It can be called from different parts of a program to avoid code repetition.

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

What is a method in programming?

A

A method is a type of subroutine associated with an object or class in object-oriented programming. It defines a behavior for objects.

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

What does the call instruction do?

A

call operand jumps to a subroutine and stores the current program counter (PC) value (the return address) before jumping.

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

What does the ret instruction do?

A

‘ret’ retrieves the stored return address and loads it back into the program counter (PC), resuming execution after the subroutine.

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

How is ‘call’ different from a regular ‘jump’ instruction?

A

‘call’ saves the current location (return address) before jumping, while a regular ‘jump’ simply moves to a new location without saving.

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

Where is the return address typically stored during a ‘call’ operation?

A

The return address is usually stored in a register or on the stack to be retrieved later by the ‘ret’ instruction.

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

What is one idea for storing the return address in a subroutine call?

A

The first byte of the subroutine can be used to store the return address.

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

Why might storing the return address in the subroutine’s first byte be problematic?

A

It can overwrite important instructions or data at the start of the subroutine, making the subroutine unreliable.

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

What is the return address?

A

It is the memory location where the program should continue execution after a subroutine finishes.

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

In simple systems, why is using the first byte for the return address considered?

A

It simplifies design by avoiding the need for a dedicated return address stack or register.

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

What is a major disadvantage of storing the return address in the first byte of a subroutine?

A

Only one return address can be stored.

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

What happens if a subroutine that uses Idea #1 tries to call itself (recursion)?

A

It would need to store a second return address, which Idea #1 cannot handle.

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

Why can’t a subroutine call itself under Idea #1?

A

Because overwriting the single return address would lose the original return location, breaking program execution.

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

What programming feature becomes impossible if only one return address can be stored?

A

Recursion (a subroutine calling itself).

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

What does FORTRAN stand for, and what were its features?

A

FORTRAN stands for “FORmula TRANslation”; it used jumps (GOTO) and conditional jumps but banned recursion.

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

Why did FORTRAN ban recursion?

A

To support a simple return address storage method like storing it in the first byte (Idea #1).

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

What was Algol, and how was it different from FORTRAN?

A

Algol (“Algorithmic Language”) introduced structured programming (if..then..else, etc.) and allowed recursion.

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

Which early language’s ideas influenced modern languages like C and Java?

A

Algol’s ideas influenced the design of modern programming languages.

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

What is a stack in computer science?

A

A stack is a structure that can flexibly store a variable number of bytes, handling data dynamically.

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

What does LIFO mean in the context of stacks?

A

LIFO stands for “Last In, First Out” — the last item added to the stack is the first one removed.

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

What is another name for LIFO?

A

FILO, which stands for “First In, Last Out.”

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

Why are stacks useful for storing return addresses?

A

They can store multiple return addresses and retrieve them in the correct order, allowing recursion and nested subroutine calls.

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

What is the purpose of the sp (stack pointer) register?

A

The ‘sp’ register points to the top of the stack in memory.

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

Do we need to know where the bottom of the stack is?

A

No, as long as we are careful to only pop values that have been pushed.

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

What can happen if you pop from the stack without pushing first?

A

It can lead to errors like retrieving invalid data or causing crashes.

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

What are the steps to push a value onto the stack?

A

Write the value to memory at address sp, then add 1 to sp.

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

How does the CPU interact with the stack in memory?

A

The CPU uses the stack pointer (sp) to keep track of where data is pushed and popped.

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

What are the steps to pop a value from the stack?

A

Subtract 1 from sp, then read the value from memory at the address sp.

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

When pushing a value, why do we increment the stack pointer (sp) after writing?

A

So the sp always points to the next free location on the stack.

29
Q

When popping a value, why do we decrement the stack pointer (sp) first?

A

To point back to the last pushed value, ensuring correct retrieval.

29
Q

What is the first step when pushing a value X onto the stack?

A

Write the value X to memory at the address pointed to by sp.

30
Q

After writing the value to memory during a push, what is the next step?

A

Add 1 to the stack pointer (sp).

31
Q

Why do we increment sp after a push operation?

A

To move the stack pointer to the next empty memory location.

32
Q

What would happen if you forgot to increment sp after a push?

A

The next push would overwrite the same memory location, causing data loss.

33
Q

What is Idea #2 for storing the return address?

A

Store the return address on a stack.

34
Q

Why is storing the return address on a stack a better approach than using a fixed memory location (like the first byte)?

A

The stack can dynamically store multiple return addresses, allowing for recursion and nested function calls.

35
Q

What advantage does using the stack for return addresses provide for recursion?

A

It allows each recursive call to store its return address separately, so each function call can return to the correct location.

36
Q

How does the stack work to manage return addresses during nested calls?

A

Each return address is pushed onto the stack during a subroutine call and popped off in reverse order when returning from the subroutine.

37
Q

Why do we need to save registers during subroutine calls?

A

Subroutines may use registers for calculations, but we need to restore the previous values of those registers when the subroutine returns.

38
Q

What is the common pattern for saving and restoring registers in subroutines?

A

The pattern is to push all registers onto the stack at the beginning and pop them back before returning.

39
Q

What is a “stack frame” in the context of subroutine calls?

A

A stack frame includes the return address and any saved registers, allowing the subroutine to work without affecting the calling function’s state.

40
Q

How do Java method calls relate to the concept of a stack frame?

A

Java method calls develop the idea of stack frames by saving the method’s state (parameters, return address, and registers) to ensure proper execution flow and return.

41
Q

What is Reverse Polish Notation (RPN)?

A

RPN is a mathematical notation where the order of operations is as written, without the need for parentheses.

42
Q

Why are parentheses unnecessary in Reverse Polish Notation?

A

Because RPN defines the order of operations explicitly, eliminating the need for brackets to indicate precedence.

43
Q

How does a stack work in Reverse Polish Notation?

A

The stack stores intermediate results. Numbers and variables are pushed onto the stack, and operations are applied to the top elements of the stack.

44
Q

What happens when an operation is encountered in RPN?

A

The operation is applied to the top elements of the stack, and the result is pushed back onto the stack.

45
Q

What are the initial steps in converting an infix expression to postfix?

A

Initialise an empty stack for operators and an empty list for the output.

46
Q

How do you handle operands (numbers or variables) during the conversion?

A

If the token is an operand, add it directly to the output list.

47
Q

What do you do when you encounter an operator in the infix expression?

A
  1. While the stack is not empty, pop operators from the stack and add them to the output if their precedence is greater than or equal to the current operator.
  2. Push the current operator onto the stack.
48
Q

How are parentheses handled during the conversion?

A
  • For an opening parenthesis ‘(’, push it onto the stack.
  • For a closing parenthesis ‘)’, pop from the stack and add to the output until an opening parenthesis is encountered, then discard the opening parenthesis.
49
Q

What is the final step after processing the entire infix expression?

A

Pop any remaining operators from the stack and append them to the output list to get the final postfix expression.

50
Q

Can any expression be converted to Reverse Polish Notation (RPN)?

A

Yes, any expression can be converted to RPN, making it easy to execute using a stack.

51
Q

What are some real-world applications of Reverse Polish Notation?

A
  • Humans can use RPN directly (e.g., in calculators).
  • Some pocket calculators from the 1970s, like the HP-41C, used RPN.
    (See more details on classic calculators here).
52
Q

What is the Forth programming language’s approach to stacks?

A

Forth uses two stacks:

  1. Operand stack for calculations.
  2. Return stack for module calls.
53
Q

Where can you learn more about the Forth programming language and its use of stacks?

54
Q

How is Reverse Polish Notation used in compilers?

A

Compilers often convert expressions into RPN form, which is then executed directly. This simplifies evaluation and execution.

55
Q

Can you give an example of a system that uses Reverse Polish Notation for execution?

A

Postscript format (used for printable files) is an example. Postscript uses RPN, and printers execute these RPN instructions to render documents.

56
Q

How is RPN used in Java bytecode?

A

Java bytecode uses operand stacks for calculations. Each Java bytecode instruction operates on an operand stack, allowing efficient computation.

57
Q

How does Java handle operand stacks in method calls?

A

In Java, each method call has its own operand stack, which is used to evaluate expressions and handle local variables during the method’s execution.

58
Q

What are the two stacks used in a stack machine?

A
  1. Return stack for subroutine returns.
  2. Operand stack for Reverse Polish Notation (RPN) calculations.
59
Q

Why don’t stack machines need registers like a, b, and c?

A

Because all operations are performed using the stacks, which store operands and results instead of using separate registers.

60
Q

What is the advantage of using stacks instead of registers in a stack machine?

A
  • More space for calculations: The stacks can dynamically store a large number of operands.
  • Opcodes don’t need to specify registers: The operations just refer to the top of the stack, simplifying the instruction set.
61
Q

What is a disadvantage of using stacks instead of registers in a stack machine?

A

It can be harder to know where things are on the stack, making it more difficult to track operands and results during execution.

62
Q

What is the underlying meaning of an “operand”?

A

An operand is whatever an operator operates on; it’s the target of an operation.

63
Q

What is the first meaning of an operand?

A

An operand can be the extra bytes after the instruction opcode in memory. For example, in the instruction ld a 42, 42 is the operand.

64
Q

What is the second meaning of an operand?

A

In the context of a stack machine, operands refer to entries in the operand stack. These are the values used for calculations in Reverse Polish Notation (RPN).

65
Q

How should you avoid confusion when thinking about operands?

A

Remember that operands can refer either to memory values (like in the instruction ld a 42) or to values on the operand stack in a stack machine (used for RPN calculations).

66
Q

What happens during an unconditional jump in a stack machine?

A

An unconditional jump does not use the operand stack. The program simply jumps to a new location in the code, and no operands are involved in this operation.

67
Q

What is the role of the operand stack in an unconditional jump?

A

The operand stack is not used in an unconditional jump. The jump is purely based on the program counter (PC) and not dependent on stack values.

68
Q

What are conditional jumps?

A

A conditional jump is a jump that only occurs if a certain condition is met, often based on the value of the operand stack or a specific comparison.

69
Q

Does the operand stack play a role in conditional jumps?

A

Yes, conditional jumps usually depend on the values on the operand stack to decide whether the jump should happen, making the stack a crucial part of the condition evaluation.