{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS 201: Computer Architecture\n", "

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Computer Architecture I.

\n", "\n", "

\n", "

Summary:

\n", "\n", "\n", "\n", "> Perlis epigram #44: Sometimes I think the only universal\n", "in the computing field is the fetch-execute cycle.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The term \"random access memory\" conveys that the time to\n", "access any particular value in the memory is (essentially) the same,\n", "as opposed to a \"sequential access memory\" like disk or\n", "magnetic tape, where accessing \"the next\" value can be\n", "considerably less time-consuming than accessing an\n", "arbitrary value (because of the travel time of the physical\n", "media, eg, radial movement of a disk head, the spinning of the disk, \n", "or the winding of the tape.)\n", "\n", "

\n", "We abstract the interface for a random access memory (RAM) as follows.\n", "There are two special registers, the memory address register (MAR)\n", "and the memory data register (MDR), and a signal indicating\n", "whether to read from the memory or write to the memory.\n", "The memory itself consists of a number n = 2A of memory registers,\n", "each of which holds B bits.\n", "Then the MDR is also B bits (it can hold the value of one memory\n", "register) and the MAR is A bits (it can hold the address -- a number\n", "between 0 and 2A-1 -- of any one of the memory registers.)\n", "When the operation is a read, the contents of the memory register\n", "addressed by the MAR is copied to the MDR.\n", "When the operation is a write, the contents of the MDR is copied\n", "to the memory register addressed by the MAR, replacing its\n", "previous contents.\n", "(In the \"tiny RAM\" example of the previous lecture, there was no\n", "MDR, and we had A = 2 address bits, n = 2A = 4 memory registers,\n", "and B = 4 bits in each memory register.)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use this interface to read the contents of a register from RAM,\n", "we put the address of the register in the MAR and send the \"read\" signal.\n", "This causes the contents of the addressed memory register to be\n", "copied to the MDR, where we may access it.\n", "To place a particular value in a particular memory register of the RAM,\n", "we put the address of the register in the MAR, and put the value\n", "we wish to store in the MDR and send the \"write\" signal.\n", "This causes the contents of the MDR to be copied into the addressed\n", "memory register.\n", "(The previous contents of that memory register are *replaced* by\n", "the new contents.)\n", "These operations are at the \"micro-instruction\" level, not visible\n", "to a person programming the computer at the assembly language level.\n", "

\n", "(There was an aside on how \"core memory\" works, and an\n", "example of 1024 bits of it from the Computer Museum in Boston.\n", "The 4 Gigabytes of RAM on a modern laptop were calculated to be equivalent\n", "to more than 32 million such arrays of tiny ferrite cores.)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We looked at two \"block diagrams\" of the organization of a Von Neumann\n", "computer.\n", "

\n", "\n", "

\n", "\n", "Both of them showed main memory, the arithmetic/logical unit (ALU),\n", "the control unit (CU), and input/output functions.\n", "The ALU and CU are usually grouped together into the \"central processing\n", "unit\" or CPU.\n", "The main memory is the RAM that we discussed above.\n", "Input/output covers communication of the central computer\n", "with devices such as keyboards, mice, displays, microphones,\n", "speakers, DVD players, network interfaces, and secondary storage\n", "like disks, and so on.\n", "The ALU can be thought of as a collection of combinational circuits\n", "to perform operations on data like addition, subtraction, comparison,\n", "logical operations, and the like.\n", "The CU is a collection of registers and combinational logic that\n", "collectively are responsible for fetching and executing instructions, \n", "(in some sense the conductor of the orchestra that is the computer.)\n", "Both diagrams indicated one or more \"central registers\", that is, registers\n", "local to the CPU that are integral to the execution of programs by the\n", "computer.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The \"Von Neumann architecture\", in which the instructions of a program and\n", "the data for the program are both located in the same (main) memory, is\n", "generally contrasted with the \"Harvard architecture\", in which there are\n", "separate memories for the instructions of a program and its data.\n", "\n", "

\n", "\n", "

\n", "In the Von Neumann architecture, a program is run as follows.\n", "There is a CPU register called the \"instruction counter\" or \"program counter\",\n", "which holds the address of the next instruction to execute.\n", "The instruction is \"fetched\" (read from main memory into a CPU register\n", "called the \"instruction register\") and \"decoded\" (which determines what\n", "operation is specified by the instruction) and \"executed.\" \n", "The execution of one instruction may\n", "involve:\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Usually after an instruction is executed, the program counter is increased\n", "by 1, and the whole \"fetch-execute\" cycle is repeated to execute\n", "the next instruction in memory.\n", "This process continues until a halt instruction is executed.\n", "Some instructions (eg, jumps and skips) cause a different update to the\n", "program counter, altering the pattern of executing consecutive instructions.\n", "\n", "\n", "

\n", "We describe the design of the TC-201 computer (\"TC\" for \"tiny\n", "computer\" or \"toy computer\"), to make the ideas of computer\n", "architecture, machine language and assembly language concrete.\n", "It consists of 3 parts: main memory (or RAM), a central\n", "processing unit (or CPU), and an input/output system to\n", "communicate with the outside world (that is, you, the user.)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main memory, or RAM, of the TC-201 consists of 4096 memory registers,\n", "also called \"words\", of 16 bits each, addressed by integers from 0 to\n", "4095.\n", "Because 4096 = 212, 12 bits are sufficient to represent any memory\n", "address.\n", "There is a central register: the accumulator (or ACC), which has 16 bits.\n", "The other CPU state consists of the program counter (or PC), which has 12 bits\n", "and holds the address of the next instruction to be executed,\n", "the run-flag, which is one bit and indicates whether the computer\n", "is running (1) or halted (0), and the arithmetic error bit (or AEB), which\n", "indicates whether there has been an arithmetic overflow error.\n", "\n", "\n", "

\n", "

TC-201 instructions.

\n", "Each TC-201 instruction occupies 16 bits in the RAM.\n", "The bits are numbered 0 to 15 from left to right.\n", "The top four bits, 0 through 3, are the operation code (or \"opcode\") \n", "and determine which operation will be executed.\n", "Thus, there could be 24 = 16 possible different opcodes.\n", "Most of the instructions involve the contents of the accumulator,\n", "and some of them also involve the contents of a word in memory.\n", "In that case, the remaining 12 bits, 4 through 15, are the address\n", "of the memory register that will be involved in the operation.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The TC-201 instructions mentioned in this lecture:\n", "
\n",
    "  opcode  operation\n",
    "  ------  ---------\n",
    "  0000    halt -- stops execution (sets the run flag to 0)\n",
    "  0001    load -- copies contents of addressed memory register to accumulator\n",
    "  0010    store -- copies contents of accumulator to addressed memory register\n",
    "
\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we denote the contents of the accumulator by acc,\n", "the address field of the instruction by addr, and the contents\n", "of the memory register with address addr by Mem[addr], we can\n", "abbreviate the function of load as acc := Mem[addr] and the function\n", "of store as Mem[addr] := acc, where := is an assignment operator." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Computer Architecture II.

\n", "\n", "

\n", "

Summary:

\n", " \n", "

\n", "\n", "

\n", "In the TC-201 we have 16 bits to represent an integer.\n", "There are 216 = 65536 possible patterns of 16 bits,\n", "so we could represent that many different integers.\n", "If we needed only non-negative integers, a natural\n", "choice would be \"unsigned binary\", which would just\n", "be the 16-bit binary representations of the numbers\n", "0 through 65,535 as the patterns 0000000000000000 through\n", "1111111111111111, respectively.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However,\n", "we'd like to represent a range of positive and negative\n", "integers, but the number of integers from -n to n is 2n+1,\n", "an odd number.\n", "As a result, the number representations we'll consider each\n", "have an \"anomaly\" because the number of patterns is even.\n", "The three systems we consider are: sign/magnitude, one's complement,\n", "and two's complement.\n", "For illustration, we'll consider the systems using just 4 bits (instead\n", "of 16) -- the principles apply to any number of bits, for example, the\n", "more common 32 or 64 bits of modern computers.\n", "With 4 bits we have 24 = 16 possible bit patterns.\n", "Here is what they represent, in unsigned binary and in\n", "each of the three systems we consider.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n",
    "  bit pattern   unsigned binary  sign/magnitude   one's complement   two's complement\n",
    "  -----------   ---------------   --------------   ----------------   ----------------\n",
    "    0000               0                 0                 0                 0\n",
    "    0001               1                 1                 1                 1\n",
    "    0010               2                 2                 2                 2\n",
    "    0011               3                 3                 3                 3\n",
    "    0100               4                 4                 4                 4\n",
    "    0101               5                 5                 5                 5\n",
    "    0110               6                 6                 6                 6\n",
    "    0111               7                 7                 7                 7\n",
    "    1000               8                -0*               -7                -8*\n",
    "    1001               9                -1                -6                -7\n",
    "    1010              10                -2                -5                -6\n",
    "    1011              11                -3                -4                -5\n",
    "    1100              12                -4                -3                -4\n",
    "    1101              13                -5                -2                -3\n",
    "    1110              14                -6                -1                -2\n",
    "    1111              15                -7                -0*               -1\n",
    "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(The asterisk (*) indicates the \"anomaly\" in each system we consider: a second\n", "representation of 0 in sign/magnitude and one's complement, and\n", "a number, -8, with no corresponding positive number in the representation,\n", "in two's complement.)\n", "Note that all three systems represent the numbers 0 through 7 in\n", "the same way (except for the extra representations of 0) -- as\n", "0 followed by the 3-bit unsigned binary representation of the\n", "integer; this is also the unsigned binary representation of these numbers.\n", "(In general, for b bits, the numbers 0 through 2b-1-1 are represented\n", "as 0 followed by the (b-1)-bit unsigned binary representation of the\n", "number.)\n", "The three systems differ in how they represent negative numbers.\n", "

\n", "For sign/magnitude, the representation of a negative number is 1 followed\n", "by the 3-bit unsigned binary representation of the absolute value of the\n", "number.\n", "Thus, the high-order bit is a sign bit (0 for + and 1 for -), and the\n", "remaining bits give the magnitude (or absolute value) of the number.\n", "To convert a positive to a negative number, it suffices to change\n", "the high-order bit from 0 to 1, and to convert a negative to a positive\n", "number, it suffices to change the high-order bit from 1 to 0.\n", "Note that we have two representations of 0, namely 0000 and 1000,\n", "that latter being referred to as -0.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "For one's complement, the negative of a number is found by\n", "complementing each bit individually (changing 0 to 1 and 1 to 0.)\n", "Thus, we represent -1 in 4-bit one's complement by complementing\n", "each bit of 0001 to get 1110.\n", "Of course, since complementation is self-inverse (the complement of\n", "the complement is the original argument), the negative of a negative\n", "number is also found by complementing each bit.\n", "In one's complement (also) the number zero has two representations: \n", "0000 and 1111 -- the latter being referred to as -0.\n", "

\n", "\n", "

\n", "The rationale for two's complement is that it implements arithmetic\n", "modulo 2b, where b is the number of bits.\n", "If we consider the integers 0 through 15 modulo 16, then we have\n", "1+15 = 0 (modulo 16) and 5+11 = 0 (modulo 16).\n", "Thus, modulo 16, 15 \"behaves like\" -1, in the sense that 1 and 15 add\n", "to 0 (modulo 16), and 11 \"behaves like\" -5.\n", "If we consider the 4-bit unsigned representation of 15, we have 1111,\n", "which is the representation of -1 in 4-bit two's complement arithmetic,\n", "and the unsigned 4-bit representation of 11 is 1011, which is the\n", "representation of -5 in 4-bit two's complement.\n", "The number 8 is a bit anomalous modulo 16, because 8+8 = 0 (modulo 16).\n", "The representation 1000 is taken to be -8, which means that the\n", "high-order bit signifies whether the number is negative (if the bit\n", "is 1) or non-negative (if the bit is 0.)\n", "Why would two's complement be desirable?\n", "If you recall the circuit we designed to add two 4-bit numbers to get\n", "a 5-bit sum, if we just ignore the high-order bit of the result, the\n", "circuit computes the sum of its two inputs, modulo 16.\n", "Two's complement is in fact the most common choice for the\n", "representation of integers in modern computers.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "The rule some have learned for negating a two's complement number is\n", "to complement each bit individually and then add 1.\n", "For example, to complement 5, we start with 0101 and complement each\n", "bit: 1010, and then add 1: 1011, which is the correct representation\n", "of -5 in two's complement.\n", "Going the other direction, from -5, we start with 1011, complement each\n", "bit: 0100, and add 1, to get 0101.\n", "Why does this work?\n", "Hint: complementing each bit is equivalent to subtracting the\n", "unsigned value of the number from 15.\n", "

\n", "In a class vote (years ago), it was decided that the TC-201 computer\n", "would have sign/magnitude representation of integers.\n", "We also decided that skipzero will skip on both positive\n", "zero (0000 0000 0000 0000) and \"negative zero\" (1000 0000 0000 0000).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "Next, we turn to the following task: read in a zero-terminated\n", "sequence of numbers, print them out in reverse, and then halt.\n", "A sample interaction with the user might look like the following.\n", "

\n",
    "    input = 17\n",
    "    input = -13\n",
    "    input = 22\n",
    "    input = 0\n",
    "    output = 22\n",
    "    output = -13\n",
    "    output = 17\n",
    "
\n", "We start with the following idea, which is NOT a solution.\n", "
\n",
    "    read-num:  input\n",
    "               skipzero\n",
    "               jump store-num\n",
    "               jump produce-output\n",
    "    store-num: store num\n",
    "               jump read-num\n",
    "    num:       data 0\n",
    "
\n", "The reason this doesn't work is that the first number read in\n", "will be stored in num, and then the second number read in will\n", "be stored in num (overwriting the first number) and then the third\n", "number will be stored in num (overwriting the second number), and\n", "so on.\n", "When this jumps to the code for produce-output, only the latest\n", "number read in will be available in num, and we won't have\n", "the information we need to print out the reverse of the sequence\n", "of numbers read in.\n", "(See the next lecture for how we solve this problem.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Computer Architecture III.

\n", "\n", "

\n", "

Summary:

\n", " \n", "

\n", "\n", "

\n", "In the preceding lecture, we considered the problem of\n", "reading in a zero-terminated sequence of numbers, printing\n", "out the reverse of the sequence, and halting.\n", "Here we temporarily simplify the task to reading in a zero-terminated\n", "sequence of numbers, storing them in consecutive memory locations,\n", "and then halting.\n", "(The task of printing out the reverse of the sequence will be deferred\n", "to the homework.)\n", "In the last lecture, we considered the following, NOT a solution.\n", "

\n",
    "    read-num:  input\n",
    "               skipzero\n",
    "               jump store-num\n",
    "               halt\n",
    "    store-num: store num\n",
    "               jump read-num\n",
    "    num:       data 0\n",
    "
\n", "This is not a solution because each number read in overwrites\n", "the previous one, so that at the end only the most recently\n", "read number will be available.\n", "What we need is a way of storing into num the first time\n", "through the loop, storing into the next memory location the\n", "next time through the loop, and so on.\n", "

\n", "\n", "

\n", "We considered two possible solutions to this problem.\n", "The first, highly deprecated, solution is to write self-modifying\n", "code.\n", "That is, we may treat the store instruction as data, load it into\n", "the accumulator, add 1 to it, and store it back into its memory\n", "register.\n", "This would have the effect of changing the instruction from\n", "\"store 6\" (in the original program, because num corresponds to\n", "address 6) to \"store 7\", so that the next number read in from the\n", "user would be stored in memory location 7.\n", "The code for this kind of solution would be as follows.\n", "

\n",
    "    read-num:  input\n",
    "               skipzero\n",
    "               jump store-num\n",
    "               halt\n",
    "    store-num: store num\n",
    "               load store-num\n",
    "               add constant-one\n",
    "               store store-num   \n",
    "               jump read-num\n",
    "    constant-one: data 1\n",
    "    num:       data 0 \n",
    "
\n", "Once your TC-201 simulator is running, you might want to\n", "try this running this program to see what it does.\n", "Self-modifying programs are very difficult for human beings\n", "to understand, debug, and maintain, because of the large\n", "amount of \"state\" the human has to keep track of -- not only\n", "the current data values, but also the current version of the\n", "program that is running. (See juggling eggs)\n", "

\n", "(When computer memory was scarce, self-modifying code effectively\n", "recycled memory addresses - like a studio apartment where you cook,\n", "eat, work, and sleep in the same room. You had to make do with\n", "what you had.)\n", "

\n", "So instead we introduce the last two instructions for the TC-201\n", "computer, which allow us to implement \"pointers\" in our programs.\n", "[Note: we also have the following instructions: shift,\n", "and, and xor, described in hw6.rkt.]\n", "A pointer indicates where in memory an operation is to be\n", "performed.\n", "The two instructions are as follows.\n", "

\n",
    "    name    opcode     operation\n",
    "    ----    ------     ---------\n",
    "    loadi    1011      acc := Mem[extract-address(Mem[addr])]\n",
    "    storei   1100      Mem[extract-address(Mem[addr])] := acc\n",
    "
\n", "The names stand for \"load indirect\" and \"store indirect\".\n", "The extract-address() operation indicated above means that\n", "we extract the rightmost 12 bits of a 16-bit quantity and treat\n", "it as the address of a register in RAM.\n", "

\n", "

\n", "Thus, the instruction \"loadi addr\" is executed as follows:\n", "read from memory the contents of the register with address addr,\n", "and take the rightmost 12 bits of that as another address,\n", "addr'.\n", "Copy the contents of the memory register with address addr' to the\n", "accumulator.\n", "Finally, increment the program counter, as usual.\n", "Similarly, the instruction \"storei addr\" is executed as follows:\n", "read from memory the contents of the register with address addr,\n", "and take the rightmost 12 bits of that as another address, addr'.\n", "Copy the contents of the accumulator to the memory register with\n", "address addr'.\n", "

\n", "\n", "

\n", "The following example may help illuminate the function of loadi\n", "and storei.\n", "We will simulate the TC-201 for three instructions, starting with\n", "the following configuration.\n", "

\n",
    "    acc:   0000 0000 0000 0000\n",
    "    pc:         0000 0000 0000\n",
    "    rf:                      1\n",
    "    aeb:                     0\n",
    "\n",
    "\n",
    "address    contents of memory register addressed\n",
    "-------    -------------------------------------\n",
    "    0      1011 0000 0000 0101\n",
    "    1      1100 0000 0000 0110\n",
    "    2      0000 0000 0000 0000\n",
    "    .\n",
    "    .\n",
    "    5      0000 0000 0011 0100\n",
    "    6      0000 0000 0011 0111\n",
    "    .\n",
    "    .\n",
    "    52     0000 1111 0000 1111\n",
    "    53     1010 1010 1010 1010\n",
    "    54     1111 0000 1111 0000\n",
    "    55     0101 0101 0101 0101\n",
    "
\n", "Because the pc contains address 0, we read the instruction at\n", "address 0: 1011 0000 0000 0101, decode the opcode 1011 to find\n", "that it is a loadi instruction.\n", "We take the address field of the instruction, 0000 0000 0101,\n", "which is address 5, and read the contents of memory register 5,\n", "which is 0000 0000 0011 0100.\n", "We take the rightmost 12 bits of that, 0000 0011 0100, and interpret\n", "it as address 52.\n", "Then the contents of memory register 52 are copied to the accumulator,\n", "and the program counter is incremented by 1, to produce the\n", "following configuration after the loadi instruction completes.\n", "
\n",
    "    acc:   0000 1111 0000 1111\n",
    "    pc:         0000 0000 0001\n",
    "    rf:                      1\n",
    "    aeb:                     0\n",
    "\n",
    "\n",
    "address    contents of memory register addressed\n",
    "-------    -------------------------------------\n",
    "    0      1011 0000 0000 0101\n",
    "    1      1100 0000 0000 0110\n",
    "    2      0000 0000 0000 0000\n",
    "    .\n",
    "    .\n",
    "    5      0000 0000 0011 0100\n",
    "    6      0000 0000 0011 0111\n",
    "    .\n",
    "    .\n",
    "    52     0000 1111 0000 1111\n",
    "    53     1010 1010 1010 1010\n",
    "    54     1111 0000 1111 0000\n",
    "    55     0101 0101 0101 0101\n",
    "
\n", "Now the program counter holds address 1, so we read the\n", "instruction at address 1, which is 1100 0000 0000 0110.\n", "We decode the opcode, 1100, and find that it is a storei instruction.\n", "We take the address field, 0000 0000 0110, which is address 6,\n", "and read from address 6, getting the contents 0000 0000 0011 0111.\n", "We take the rightmost 12 bits of that as an address, address 55,\n", "and copy the contents of the accumulator to memory register 55.\n", "The program counter is then incremented, resulting in the following\n", "configuration after the storei instruction has been executed.\n", "
\n",
    "    acc:   0000 1111 0000 1111\n",
    "    pc:         0000 0000 0010\n",
    "    rf:                      1\n",
    "    aeb:                     0\n",
    "\n",
    "\n",
    "address    contents of memory register addressed\n",
    "-------    -------------------------------------\n",
    "    0      1011 0000 0000 0101\n",
    "    1      1100 0000 0000 0110\n",
    "    2      0000 0000 0000 0000\n",
    "    .\n",
    "    .\n",
    "    5      0000 0000 0011 0100\n",
    "    6      0000 0000 0011 0111\n",
    "    .\n",
    "    .\n",
    "    52     0000 1111 0000 1111\n",
    "    53     1010 1010 1010 1010\n",
    "    54     1111 0000 1111 0000\n",
    "    55     0000 1111 0000 1111\n",
    "
\n", "Note that the contents of registers 5 and 6 are unchanged,\n", "but we have copied the contents of register 52 (pointed to by register 5)\n", "to register 55 (pointed to by register 6).\n", "Next, the instruction at address 2 is executed.\n", "Because it is a halt, all that happens is that the run flag (rf)\n", "is set to 0, and execution halts.\n", "

\n", "\n", "

\n", "We can make use of the idea of a pointer (which we repeatedly\n", "increment) and the storei instruction to solve the problem\n", "of reading in a zero-terminated sequence of numbers and storing\n", "them in consecutive locations in memory, and then halting.\n", "

\n",
    "    read-num:  input\n",
    "               skipzero\n",
    "               jump store-num\n",
    "               halt\n",
    "    store-num: storei pointer\n",
    "               load pointer\n",
    "               add constant-one\n",
    "               store pointer\n",
    "               jump read-num\n",
    "    pointer:   data table\n",
    "    constant-one: data 1\n",
    "    table:     data 0\n",
    "
\n", "The memory location pointer initially contains the address corresponding\n", "to table.\n", "Thus, the first number read in from the user will be stored\n", "in the memory register corresponding to table.\n", "Then the sequence of instructions adds one to the value of the\n", "pointer, so that it now holds the address of the memory register\n", "after table.\n", "Thus, the next number read in will be stored in the next memory\n", "location, and so on, until a zero is input by the user.\n", "

\n", "

\n", "To see why this solution works in a little more detail, we first\n", "consider how the assembler that you will write for the homework\n", "will translate the above program into a sequence of 16-bit patterns\n", "to store in the RAM starting at address 0.\n", "Conceptually, the assembler first needs to determine how to\n", "translate all the symbolic addresses in the program into numbers.\n", "To do this, it merely numbers the instructions and data statements\n", "starting with 0, to determine the addresses that the corresponding\n", "instructions or data elements will have.\n", "Numbering the instructions and data statements of the above program,\n", "we have the following.\n", "

\n",
    "0    read-num:  input\n",
    "1               skipzero\n",
    "2               jump store-num\n",
    "3               halt\n",
    "4    store-num: storei pointer\n",
    "5               load pointer\n",
    "6               add constant-one\n",
    "7               store pointer\n",
    "8               jump read-num\n",
    "9    pointer:   data table\n",
    "10   constant-one: data 1\n",
    "11   table:     data 0\n",
    "
\n", "From this we can extract a \"symbol table\" allowing us to translate\n", "the symbolic labels to numbers:\n", "
\n",
    "   label          corresponding address\n",
    "   -----          ---------------------\n",
    "   read-num       0\n",
    "   store-num      4\n",
    "   pointer        9\n",
    "   constant-one   10\n",
    "   table          11\n",
    "
\n", "With this table in hand, we can now translate the sequence of\n", "instructions and data statements one by one into the corresponding\n", "16-bit patterns.\n", "For an instruction, we look up the 4 bit opcode corresponding to\n", "the name of the instruction, and, if the instruction has an address\n", "field, we convert the numeric address into a 12-bit pattern to\n", "combine with the opcode.\n", "If there is no address field, we fill the remaining 12 bits with 0's.\n", "For a data statement, we take the number after the \"data\" field\n", "and convert it, using 16-bit sign/magnitude representation, into\n", "a 16-bit pattern.\n", "

\n", "

\n", "For example, the first instruction is \"input\" (which takes no\n", "address field), so the resulting bit pattern is 0101 0000 0000 0000.\n", "The second instruction, \"skipzero\" is translated into\n", "1000 0000 0000 0000, because the skipzero opcode is 1000.\n", "The third instruction is \"jump store-num\". The opcode is 0111,\n", "for jump, and looking up store-num in the symbol table, we\n", "find it is address 4, which we put in the address field in binary,\n", "to get the pattern 0111 0000 0000 0100.\n", "For the data statement \"data 1\" we convert 1 to a 16-bit quantity\n", "in sign/magnitude representation to get the pattern 0000 0000 0000 0001.\n", "For the data statement \"data table\", we look up table in the symbol\n", "table and find that it is address 11, which we convert to 16 bit\n", "sign/magnitude representation, giving the pattern 0000 0000 0000 1011.\n", "Putting all this together, the complete translation of the above\n", "program would be as follows.\n", "

\n",
    "addr     assembly language         machine language\n",
    "---- -------------------------    --------------------\n",
    "0    read-num:  input             0101 0000 0000 0000\n",
    "1               skipzero          1000 0000 0000 0000\n",
    "2               jump store-num    0111 0000 0000 0100\n",
    "3               halt              0000 0000 0000 0000\n",
    "4    store-num: storei pointer    1100 0000 0000 1001\n",
    "5               load pointer      0001 0000 0000 1001\n",
    "6               add constant-one  0011 0000 0000 1010\n",
    "7               store pointer     0010 0000 0000 1001\n",
    "8               jump read-num     0111 0000 0000 0000\n",
    "9    pointer:   data table        0000 0000 0000 1011\n",
    "10   constant-one: data 1         0000 0000 0000 0001\n",
    "11   table:     data 0            0000 0000 0000 0000\n",
    "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we have arranged for the table to be the last\n", "piece of data in the program, because numbers read from the user\n", "will be stored in consecutive memory locations starting with\n", "table.\n", "We simulated this program for one loop to understand a\n", "bit better how it works.\n", "\n", "\n", "

\n", "We spent a little while discussing how to extend this\n", "program to prt in reverse the sequence of numbers read\n", "in before halting.\n", "The basic idea is to use \"loadi pointer\" to get the number into the\n", "accumulator and \"output\" to print it.\n", "Then the pointer is decreased by 1 and the loop is repeated.\n", "To know when to stop, there were two proposals -- one using\n", "a sentinel of 0 (which the user cannot cause to be stored\n", "in the table) to mark the beginning of the table, and the\n", "other to save the original start of the table in a separate\n", "variable, so that the pointer could be compared to it.\n", "(Note that in the above program, pointer points to the next\n", "available location in memory, not to the last stored number.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Racket", "language": "racket", "name": "racket" }, "language_info": { "codemirror_mode": "scheme", "file_extension": ".rkt", "mimetype": "text/x-racket", "name": "Racket", "pygments_lexer": "racket", "version": "7.4" } }, "nbformat": 4, "nbformat_minor": 4 }