TAOCP 2.1 Exercise 8

Steps B1-B3 insert a new card at the top of the linked pile.

Section 2.1: Introduction

Exercise 8. [18] Write a MIX program corresponding to steps B1-B3.

Verified: no
Solve time: -


Solution

Steps B1-B3 insert a new card at the top of the linked pile. Using the notation of the text, TOP contains the location of the first card, and the link field of a node is the field LINK.

A MIXAL program corresponding to these steps is

TOP     CON     0

B1      IN      NEWCARD(CARDREADER)
B2      LDA     TOP
        STA     NEWCARD(LINK)
B3      LDA     =NEWCARD=
        STA     TOP

The instruction

LDA TOP
STA NEWCARD(LINK)

implements

$$ \operatorname{LINK}(\operatorname{NEWCARD}) \leftarrow \operatorname{TOP}. $$

The instructions

LDA =NEWCARD=
STA TOP

implement

$$ \operatorname{TOP} \leftarrow \operatorname{NEWCARD}. $$

Hence the resulting list begins at NEWCARD, and the former top card becomes its successor. This is exactly the action prescribed by steps B1-B3.

$$ \boxed{\text{The MIXAL program above implements steps B1-B3.}} $$