About

Homework: Exercise L6 (Assembly)

Theory: How to Approach These

Jump to: Q1 · Q2 · Q3


Worked Examples

Exercise Q1

Given the assembly code below:

Hex AddressLabelInstruction
100Load A
101Add One
102Jump S1
103S2,Add One
104Store A
105Halt
106S1,Add A
107Jump S2
108A,HEX 0023
109One,HEX 0001

a) List the hexadecimal code for the program (hand assemble it). b) What are the contents of the symbol table for this program? c) Write the program, compile and show the result in Marie.

PartDetail
Asks(a) 4-digit hex word per line, (b) label → address table, (c) run it and give the final values.
NeedsLoad 1, Store 2, Add 3, Halt 7, Jump 9; word = opcode digit + 3-digit address; HEX copied as is.
Look atThe labels S2, S1, A, One and their addresses; Jump changes the run order.
If it changesDifferent labels or ORG change addresses and every word using them; opcode digits stay.
Show solution ↓
Solution

How to solve

  1. Pass 1: every label gets the address of its line, giving the symbol table.
  2. Pass 2: for each instruction write opcode digit + the label’s address (Halt = 7000).
  3. Run: trace the AC, following jumps, until Halt.
AddressInstructionOpcodeOperand addressHex
SymbolAddress
Addr runInstructionAC afterMemory change

Sample answer

b) Symbol table

SymbolAddress
S2103
S1106
A108
One109

a) Hex code

AddressInstructionOpcodeOperandHex
100Load A11081108
101Add One31093109
102Jump S191069106
103S2, Add One31093109
104Store A21082108
105Halt70007000
106S1, Add A31083108
107Jump S291039103
108A, HEX 00230023
109One, HEX 00010001

c) Program for the simulator

        ORG 100
        Load A
        Add One
        Jump S1
S2,     Add One
        Store A
        Halt
S1,     Add A
        Jump S2
A,      HEX 0023
One,    HEX 0001
AddrInstructionAC afterMemory change
100Load A0023
101Add One0024
102Jump S10024
106Add A0047
107Jump S20047
103Add One0048
104Store A0048A (108) = 0048
105Halt0048

Result: AC = 0048, A = 0048 = 72. The program computes A←2A+2A \leftarrow 2A + 2 (2×35+2=722 \times 35 + 2 = 72).

Watch out: Add A at 106 adds the value stored in A (still 0023, because Store A has not run yet), not the AC.

Exercise Q2

Given the instruction set for MARIE in this chapter, decipher the following MARIE machine language instructions. (Write the assembly language equivalent.)

a) 0010000000000111 b) 1001000000001011 c) 0011000000001001

PartDetail
AsksTurn 16-bit binary words into MARIE assembly.
NeedsFirst 4 bits = opcode, last 12 bits = address; opcode table (2 Store, 3 Add, 9 Jump).
Look atSplit into four 4-bit groups; each group is one hex digit.
If it changesOnly the bits change; the method is identical.
Show solution ↓
Solution

How to solve

  1. Split the word into groups of 4 bits and convert each to hex.
  2. First hex digit is the opcode; look up its name.
  3. Remaining three digits are the address.
BinaryGroups of 4HexOpcode nameAssemblyMeaning

Sample answer

Opcode bitsAddress bitsHexOpcodeAssembly
a)00100000 0000 011120072 = StoreStore 007
b)10010000 0000 1011900B9 = JumpJump 00B
c)00110000 0000 100130093 = AddAdd 009

Meanings: a) M[007] ← AC; b) PC ← 00B; c) AC ← AC + M[009].

Watch out: the address is 12 bits (three hex digits), so keep the leading zeros.

Exercise Q3

Given the following code segment:

If X>1 then
    Y=X+X;
    X=0;
endif;
Y=Y+1;

Write the code in MARIE’s assembly language. Compile and test the program given X = 2 and Y = 10.

PartDetail
AsksWrite, assemble and run a MARIE program for the if-statement with X = 2, Y = 10.
NeedsSkipcond (000: AC < 0, 400: AC = 0, 800: AC > 0); pattern: compute, Skipcond, Jump past THEN.
Look atX>1X > 1 is X−1>0X - 1 > 0, so Skipcond 800; you need a constant One.
If it changesX<YX < Y: compute X−YX - Y, Skipcond 000. X=YX = Y: Skipcond 400. The Jump EndIf pattern stays.
Show solution ↓
Solution

How to solve

  1. Load X, Subt One gives X−1X - 1 in the AC.
  2. Skipcond 800 skips the next line when AC > 0 (condition true).
  3. The next line is Jump EndIf, so it runs only when the condition is false.
  4. THEN part: Y=X+XY = X + X, then Clear and Store X.
  5. EndIf: Y=Y+1Y = Y + 1, Halt, then the data after Halt.
If,     Load A
        Subt B           / AC = A - B
        Skipcond ___     / 800 for >, 000 for <, 400 for =
        Jump EndIf       / condition false: skip THEN
Then,   ...THEN part...
EndIf,  ...code after the if...
        Halt
A,      Dec ___
B,      Dec ___

Sample answer

        ORG 100
If,     Load X
        Subt One
        Skipcond 800    / X-1 > 0 ?
        Jump EndIf
Then,   Load X
        Add X
        Store Y         / Y = X + X
        Clear
        Store X         / X = 0
EndIf,  Load Y
        Add One
        Store Y         / Y = Y + 1
        Halt
X,      Dec 2
Y,      Dec 10
One,    Dec 1

Symbol table: If 100, Then 104, EndIf 109, X 10D, Y 10E, One 10F.

AddressHexAddressHex
100110D108210D
101410F109110E
102880010A310F
103910910B210E
104110D10C7000
105310D10D0002
106210E10E000A
107A00010F0001

Test (X = 2, Y = 10):

AddrInstructionAC afterXY
100Load X0002210
101Subt One0001210
102Skipcond 8000001 (skip 103)210
104Load X0002210
105Add X0004210
106Store Y000424
107Clear000024
108Store X000004
109Load Y000404
10AAdd One000504
10BStore Y000505
10CHalt05

Result: Y = 5, X = 0. With X = 1 the AC is 0, nothing is skipped, Jump EndIf runs and only Y=11Y = 11 results.

Watch out: Skipcond skips when the condition is true, so the next line must be the jump for the false case. Put data after Halt or the CPU runs it as code.