Show understanding that a set of instructions are grouped

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Assembly Language

4.2 Assembly Language – Grouping Instructions

Learning Objective

Demonstrate an understanding of how assembly language instructions are organised into logical groups (segments, blocks, and sub‑routines) to improve readability, maintainability and execution flow.

Why Group Instructions?

  • Clarifies the purpose of each part of the program.
  • Allows the assembler to place data and code in appropriate memory areas.
  • Facilitates reuse through sub‑routines and macros.
  • Supports debugging by isolating functional sections.

Common Groupings in Assembly

  1. Segments – Separate regions for data and code.
  2. Blocks – Sequences that implement a single algorithmic step (for example, a loop body).
  3. Sub‑routines / Procedures – Re‑usable code called via CALL and returned with RET.
  4. Macros – Textual substitution performed by the assembler.

Typical Segment Layout (MIPS Example)

.data

  msg: .asciiz "Enter a number: "

.text

  .globl main

main:

  # code here

Instruction Group Table

GroupPurposeTypical Mnemonics
Data TransferMove data between registers, memory and I/OLD, ST, MOV, LDR, STR
ArithmeticPerform integer or floating‑point calculationsADD, SUB, MUL, DIV
LogicalBitwise manipulation and condition testingAND, OR, XOR, NOT, CMP
Control FlowChange the sequential execution orderJMP, CALL, RET, BEQ, BNE
SystemInteract with the operating system or hardwareINT, S \cdot C, HLT

Example: Grouped Assembly Program (Simple Counter)

; ---------- Data Segment ----------

.data

  count: .word 0 ; variable to hold the counter

  msg: .asciiz "Count = "


; ---------- Text Segment ----------

.text

  .globl main

main:

  ; Initialise counter

  LI R1, 0 ; R1 = 0

  ST R1, count


loop_start:

  ; Increment counter

  LD R2, count

  ADDI R2, R2, 1

  ST R2, count


  ; Print current value (calls sub‑routine)

  CALL print_counter


  ; Loop 5 times

  CMP R2, #5

  BLT loop_start


  ; Exit program

  MO \cdot R7, #1 ; syscall: exit

  SWI 0


; ---------- Sub‑routine ----------

print_counter:

  ; Load message address

  LDR R0, =msg

  ; Load current count

  LD R1, count

  ; System call to write (pseudo‑code)

  MO \cdot R7, #4

  SWI 0

  BX LR ; return

Key Points to Remember

  • Each segment has a specific purpose and is declared with a directive such as .data or .text.
  • Logical blocks are separated by comments and blank lines for readability.
  • Sub‑routines should preserve registers they use, unless a calling convention states otherwise.
  • Control‑flow instructions (JMP, CALL, RET) define the boundaries between groups.

Practice Question

Write a short assembly program for a hypothetical processor that:

  1. Stores three integer constants in a data segment.
  2. Calculates their sum in a separate code block.
  3. Calls a sub‑routine to display the result.

Explain how you have grouped the instructions and why the grouping aids understanding.

Suggested diagram: Flowchart showing the relationship between Data Segment, Main Code Block, Loop Block, and Sub‑routine.