NivaarExam PrepOfficial exam papers ↗

22-Elec-A4 Digital Systems and Computers · May 2013

Question 5 of 6: Big-Endian Storage and the Stack

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

Paper format. National Exams, May 2013 — 07-Elec-A4 Digital Systems & Computers. Three hours, closed book (one approved Casio or Sharp calculator). Six questions are printed; any five constitute a complete exam and all questions are worth 12 marks. An excitation table for the RS/JK/T/D flip-flops and a table of basic Boolean identities are supplied on the last page of the paper. All six questions are solved below, because this set is a study resource rather than a timed sitting.

Reference texts.

Convention used throughout. In the address/data expressions a prime denotes complement (\(\overline{A}\) is written A′ in the figures, where SVG text cannot carry an overbar). Hexadecimal constants keep the Motorola dollar-sign notation of the exam paper, written here as $7A01 in prose so that it cannot be mistaken for a mathematics delimiter.

Question 5: Big-Endian Storage and the Stack (12 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given.

ItemValue
Datum16-bit word $7A01, i.e. most-significant byte $7A, least-significant byte $01
Store address$C239 (memory drawn with low addresses at the top)
Stack pointer before PUSHSP = $DC51
Byte orderBig-endian (Motorola convention)
Stack growthToward low memory, SP addressing the next free byte
Marks(a) 4, (b) 4, (c) 4

Find. The byte written into each drawn location by the STORE and by the PUSH, and the post-PUSH contents of SP.

Approach. Apply the two independent conventions in turn: big-endianness fixes which byte goes at which address, while the stack discipline fixes which addresses are used and how SP moves. Both must be applied to part (b); part (a) needs only the first.

  1. Split the datum. A 16-bit word occupies two 1-byte locations. Splitting $7A01, $$\text{MS byte} = \text{7A}_{16}, \qquad \text{LS byte} = \text{01}_{16}$$ Big-endian order means the big end of the number — its most significant byte — is stored at the lowest address of the pair, and the address quoted in the instruction is that lowest address.
  2. (a) Perform the STORE. The instruction names $C239, so that location takes the most significant byte and the next location up takes the least significant byte: $$(\text{C239}_{16}) \leftarrow \text{7A}_{16}, \qquad (\text{C23A}_{16}) \leftarrow \text{01}_{16}$$ Location $C238 lies below the named address and is not touched by this instruction.
    1-byte locationsLow Memory...0xC2380xC2390x7AMS byte (big-endian)0xC23A0x01LS byte...High Memory
    Question 5(a) - memory contents after storing $7A01 to address $C239 in big-endian order.
    Reading the two bytes back in increasing-address order reproduces 7A 01, which is the printed form of the number — the practical reason big-endian ordering is convenient for hexadecimal memory dumps.
  3. (b) Perform the PUSH. A Motorola-style stack grows downward in memory and SP points at the next free byte. Pushing a 16-bit quantity is therefore two byte-pushes, least significant byte first:
    Micro-stepActionResult
    1$(\text{SP}) \leftarrow$ LS byte($DC51) = $01
    2$\text{SP} \leftarrow \text{SP} - 1$SP = $DC50
    3$(\text{SP}) \leftarrow$ MS byte($DC50) = $7A
    4$\text{SP} \leftarrow \text{SP} - 1$SP = $DC4F
    Pushing the low byte first is what makes the pair land big-endian: because each push moves to a lower address, the byte pushed last — the most significant one — ends up at the lower address, exactly as in part (a). Location $DC52 is below the stack top and is untouched.
    1-byte locationsLow Memory...0xDC4FSP after PUSH0xDC500x7AMS byte0xDC510x01LS byte (SP before PUSH)0xDC52...High Memory
    Question 5(b) - stack contents after pushing $7A01 with SP = $DC51 before the push.
  4. Cross-check the stored word. Reading the two occupied bytes in increasing-address order gives $$(\text{DC50}_{16})\,(\text{DC51}_{16}) = \text{7A}_{16}\;\text{01}_{16} = \text{7A01}_{16}$$ identical to the result of the STORE in part (a), confirming that the PUSH preserved big-endian order. A subsequent PULL would reverse the four micro-steps, incrementing SP first, and recover the same word.
  5. (c) Final stack pointer. Two bytes were pushed and SP decrements once per byte: $$\text{SP}_{\text{after}} = \text{DC51}_{16} - 2 = \boxed{\text{DC4F}_{16}}$$ SP again addresses the next free byte, one below the word just pushed.

Check: stack-pointer convention. The answer above uses the Motorola 6800/68HC11 convention stated by the figure, in which SP addresses the next free byte and therefore ends at $DC4F. Some processors (including the 68000 family with its predecrement addressing) define SP as pointing at the last item pushed; with that convention the first byte would go to $DC50, the second to $DC4F and SP would finish at $DC4F as well — the same final SP, but the two data bytes shifted one location down. The figure's arrow marks SP at $DC51 before the push, i.e. at a location shown as empty, which identifies the next-free-byte convention used here.

PartLocationContents after the instruction
(a)$C238unchanged
(a)$C239$7A  (most significant byte)
(a)$C23A$01  (least significant byte)
(b)$DC50$7A  (pushed second)
(b)$DC51$01  (pushed first)
(b)$DC52unchanged
(c)SP after PUSH$DC4F