4.7.3.5 - Machine-code and assembly operations (Python)

4.7.3.5 - Machine-code and assembly operations (Python)

Apply loads, arithmetic, branches, bitwise operations and shifts to a supplied assembly model. Assembly traces and mnemonic-writing tasks are written Paper 2 responses without Run. The Python simulator and source-writing tasks are formative companion applications that explore the same state changes.

Loads, stores and arithmetic

A short assembly instruction can change a register or memory value, so the first task is to establish exactly what the supplied machine model means.

Machine code represents instructions in binary. Assembly language uses mnemonic names for those instructions, such as ADD. An assembler translates mnemonic source into machine code. Mnemonics and operand conventions vary between instruction sets; the following is this lesson's supplied teaching model, not a claim that every processor or AQA question uses these exact tokens.

Our model has an 8-bit accumulator ACC, a register holding the current arithmetic/logic value, byte-valued data memory, and a program counter PC holding the address of the next instruction. Values are unsigned 0–255. Arithmetic results wrap modulo 256. ACC initially contains 0 unless stated otherwise.

InstructionMeaning in this model
LDA operandReplace ACC with the operand's value.
STA addressCopy ACC into that data-memory address; leave ACC unchanged.
ADD operandReplace ACC with ACC plus the operand's value, modulo 256.
SUB operandReplace ACC with ACC minus the operand's value, modulo 256.
HLTHalt execution; do not execute the following instruction.

An immediate operand has # before its value: LDA #20 loads the number 20 itself. A direct operand names the data-memory address: LDA 20 loads the contents of address 20. If memory[20] = 7, these instructions load 20 and 7 respectively. Direct addressing does not mean the address and its contents are equal. STA 20 stores to address 20; this model does not permit STA #20.

Here memory[40] initially contains 250:

assembly
LDA 40
ADD #10
STA 41
SUB #5
HLT

ACC becomes 250, then 4 because 260 modulo 256 is 4. STA 41 stores 4 but does not empty ACC. Subtracting 5 gives −1 before wrapping, so the final ACC is 255. Memory[40] remains 250 and memory[41] remains 4. Instructions after HLT, if present, do not execute.

Comparisons, branches and loops

Branching changes which instruction executes next, and comparison supplies a condition without changing the accumulator value.

In this model CMP operand records whether the unsigned ACC is less than, equal to, or greater than the operand's value. It leaves ACC and memory unchanged. A later conditional branch uses that recorded relation. A comparison is not the same instruction as subtraction, and the recorded relation is not updated by an ordinary load or arithmetic operation in this supplied model.

InstructionEffect on control flow
BRA labelUnconditionally make PC the labelled instruction's address.
BEQ labelBranch if the most recent comparison recorded equality.
BLT labelBranch if it recorded ACC less than the compared value.
BGT labelBranch if it recorded ACC greater than the compared value.

For every non-branch instruction other than halt, PC advances to the next instruction. A conditional branch not taken also advances to the next instruction. A taken branch replaces that normal next address. Labels name instruction addresses; they are not additional instructions. A branch target refers to an instruction address, not the data stored at that address.

This program replaces a value below 5 with 5 and otherwise preserves it:

assembly
        LDA 30
        CMP #5
        BLT small
        BRA finish
small:  LDA #5
finish: STA 31
        HLT

If memory[30] = 2, comparison records “less than”, the branch reaches small, and 5 is stored. If memory[30] = 8, the conditional branch is not taken; the unconditional branch skips the replacement, and 8 is stored. If memory[30] = 5, the comparison is equal, so the “less than” branch is again not taken.

A backward branch can make a loop. To count down from a positive value, load the counter, subtract 1, store the updated counter, compare it with 0, and branch back while greater than 0. The comparison must use the updated value. An entry test is needed when a zero counter should cause zero iterations: subtracting first would wrap 0 to 255. Trace the boundary as well as an ordinary positive input.

Bitwise logical operations and shifts

Bitwise instructions act on corresponding bit positions, whereas logical shifts move all bits and insert zeros at the vacated positions.

This model adds AND operand, OR operand and XOR operand, each replacing ACC with that bitwise result. NOT has no operand and inverts all eight ACC bits. An AND bit is 1 only when both inputs are 1; an OR bit is 1 when at least one is 1; an XOR bit is 1 when exactly one is 1. These are bitwise operations on a byte, rather than Python's whole-value Boolean and, or and not.

For ACC = 10101100 and an operand of 00001111:

OperationResultInterpretation
AND00001100Keep the low four bits; clear the high four.
OR10101111Set the low four bits; preserve the others.
XOR10100011Toggle the low four bits.
NOT on the original ACC01010011Invert every bit within the eight-bit width.

LSL #n shifts ACC left by n places; LSR #n shifts it right by n places. Both insert zeros and discard bits shifted beyond the byte. For 10101100, LSL #1 gives 01011000 and LSR #1 gives 01010110. These are logical, not sign-extending arithmetic, shifts.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account