9 - Macros, Recursion, and Digital Logic Flashcards

1
Q

How is a procedure translated into assembly code and executed?

A

During assembly, procedure code is translated ONLY once.

During execution, control is transferred to the procedure at EACH call (activation record, etc.).

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

How is a macro translated into assembly code and executed?

A

During assembly, the ENTIRE macro code is substituted for each call (expansion).

During execution, it can be invoked by NAME only (not CALL),

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

How do you define macros?

A

macroname MACRO [param1, param2, …]
statement-list
ENDM

Parameters are optional. There’s also no theoretical limit to the number of parameters, but stylistically there is.

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

How do you invoke a macro?

A

Just give the name and arguments (if any).

Each argument matches a declared parameter, and each parameter is replaced by its corresponding argument when the macro is expanded.

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

How do you define WriteString as a macro?

A
mWriteStr MACRO buffer
  push edx
  mov edx, OFFSET buffer
  call WriteString
  pop edx
ENDM
..
.code
  mWriteStr str1
  mWriteStr str2
...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you define ReadString as a macro?

A
mReadStr MACRO varName
  push ecx
  push edx
  mov edx, OFFSET varName
  mov ecx, (SIZEOF varName) - 1
  pop edx
  pop ecx
ENDM


mReadStr firstName

Notice that you set up the expected length of the string (minus 1 for the null character).

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

How would you create a macro that prints a sequence from a to b?

A
seq MACRO a, b
  LOCAL test
  LOCAL quit
  mov eax, a
  mov ebx, b
test:
  cmp eax, ebx  ;if a
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do you need to do when writing macros with jumps?

A

The problem: there will be the same labels through the code, and it might jump to the wrong ones.

Therefore, you can specify the labels as being LOCAL. MASM handles the problem by appending a unique number to the label (substitution is based on how many times you’ve called the macro).

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

Is there parameter checking for MACROS?

A

As long as there’s no size mismatch, etc., they’ll accept anything!

The problem: the macro can be called with conflicting register parameters!!

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

What are the advantages of macros over procedures?

A
  1. They are convenient and easy to understand.
  2. They execute faster than procedures (no return address, stack manipulation).
  3. They are invoked by name
  4. They do NOT need a ret statement!!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the advantages of procedures over macros?

A

If the macro is called many times, the assembler procedures “fat code”, which will be invisible to the programmer.

Therefore, use a macro only for short code that is called a “few” times and uses few registers.

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

What is recursion?

A

A procedure can call itself.

A procedure A calls procedure B, which in turn calls procedure A.

A good recursive definition must ALWAYS approach a base case (not so with getData, so that should be repetition structure).

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

What is the psuedocode for a summation formula?

A

if (a == b)
return a
else return a + summation (a+1, b)

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

What is MASM code for a summation procedure to calculate the integers from x to y?

A
summation PROC
  push ebp
  mov ebp, esp
  mov eax, [ebp+16]  ;eax = x
  mov ebx, [ebp+12]  ;eax = y
  mov edx, [ebp+8]   ;@sum in edx
  add [edx], eax
  cmp eax, ebx
  je quit    ;base case if x = y
recurse:
  inc eax
  push eax
  push ebx
  push edx
  call summation
quit:
  pop ebp
  ret 12
summation ENDP
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is stack overflow so common in recursion?

A

If you don’t use a stack frame for each recursive call, etc, your recursion might never reach its base case again.

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

Why must you pass all three parameters for a summation problem, even though two of them never change?

A

You need local copies of them so that we can use ebp as a reference place.

17
Q

What does X~Y mean?

A

R is equal to X AND NOT Y (the only truth is when X is 1 and Y is 0).

18
Q

What does R+(YZ) mean?

A

R is equal to X OR (Y AND Z). (this is only not true when both X is 0 and either/both Y or Z are 0.

19
Q

How are binary 0 and 1 represented?

A

A low voltage (e.g. 0.5v) represents 0 and a high voltage (e.g. 5.0v) represents 1.

You can “convert” low to high using gates. Gates are made of transistors. Combinations of gates are called digital circuits.

20
Q

How do digital circuits produce so many outputs?

A

ANY desired output can be produced by combinations (circuits) of these 3 gates => NOT, AND, and OR.

21
Q

What is NAND?

A

~(X AND Y), as in the only time that it is false is when both x and y are 1.

22
Q

What is NOR?

A

~(X OR Y), as in the only time that it is true is when both x and y are 0.

23
Q

What is XNOR?

A

~(X XOR Y), as in the only times it is true is when:

  1. BOTH A and B are true
  2. BOTH A and B are false
24
Q

How many possible combinations of n binary variables are there?

A

2^n possible combinations.

For example, f(A, B, C) has 8 combinations of values for A, B, and C.

25
Q

How do you turn a truth table into an equation?

A

Look at all the places where the truth table equals 1, and create disjunctive combinations (+) for each of those possibilities.

Within each combination, put a bar over the variable that is 0 and 1 for all else (e.g. if A=0, B= 0, and C =1, you’d say ~A~BC)

Now, you have gates that can implement this function as a circuit.

26
Q

How do you test all possible combinations of input to verify that the circuit implements the function?

A

Look at the truth values of a solution of the function. For example, if A = 0, B = 1, and C = 0, and that’s a solution of R = ~A~BC + ~AB~C + A~B~C + ABC.

  1. Plug in the negated values for each of the bars (e.g. 10C + 1B1 + A01 + ABC)
  2. Plug in the normal values and evaluate (e.g. 100 + 111 + 001 + 010)
  3. Simplify the equation (e.g. 0 + 1 + 0 + 0 = 1).
27
Q

What are the shapes of the three logic gates?

A

NOT - triangle with a dot.
AND - dome
OR - rocket

NAND - dome with a dot
NOR - rocket with a dot
XOR - rocket with spoiler
XNOR - rocket with spoiler and a dot

28
Q

How can circuits get simplified?

A

Multiple input gates, equivalent gates, or using boolean logic to simplify the function before you implement the circuit.

29
Q

How can you simplify

R = ~A~BC + ~AB~C + A~B~C + ABC?

A

R = ~A~BC + ~AB~C + A~B~C + ABC.

  1. First, by the distributive law, ~A(~BC+B~C) + A(~B~C + BC).
  2. Next, these are by definition ~A(B XOR C) + A(B XNOR C).
  3. Since these are also the definition of XOR, you can say this is (A XOR B XOR C).

The final definition will take only 2 gates to implement.

30
Q

What is a multiplexer?

A

A trapezoid, it seems!!

Whatever input comes in from the side will switch between the two inputs that it represents.

31
Q

What does a 4-bit comparator look like?

A

Four XOR gates between A0/B0, A1/B1, etc, all linked to a NOR gate.

If all the bits are the same, the XOR bits will output a 0, and the OR gate will produce a 0 (the NOR gate becomes a 1).

On the other hand, if any of the bits are different, the XOR bits will output a 1, and the NOR gate will output a 0.

32
Q

What are the three types of bitwise adders?

A

Half-adder: 2 inputs (corresponding bits of 2 numbers), 2 outputs (sum bit and carry-out bit).

Full-adder: 3 inputs (same as above, plus carry-in bit), 2 outputs.

Ripple adder: For 4 bits, made up of 4 full-adders from each of the positions (e.g. X1, Y1) and a carry bit from the previous position.

33
Q

What does a half adder look like?

A

A and B go into perpendicular gates.

Carry gate: AND gate, so that it only carries if both are true.

Sum gate: XOR gate, so that they only sum when only one is true (e.g. 0 if carries).

34
Q

What does a full adder look like?

A

Three inputs: A, B, and the carrry in. Generally, it’s the half-adder (XOR’s to C) that has been OR’d with a half-adder (of carry-in and result:C).

Carry out: The OR function of (A AND B) or (carryIn AND C).

Sum: The XOR function of the carryIn and C.

35
Q

What does a ripple carry adder look like?

A

Each of the full adders corresponds to the bit positions, and you start with a carry bit of 0.

Each carry bit goes into the next position. This produces the correct sum bits AND overflow if there’s a carry bit.