Data Processing and Memory Operations
Laboratory Manual
The lab manual contains the full theory, circuit diagrams, and requirements for this experiment - keep it open while you work. Download Experiment 2 PDF
Examples
Example 1: Data Processing Instructions
This example demonstrates various arithmetic and bitwise operations on registers.
AREA RESET, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20001000
DCD Reset_Handler
ALIGN
AREA MYCODE, CODE, READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
; Load values from memory into registers
LDR R1, NUM1 ; R1 = 50
LDR R2, NUM2 ; R2 = 12
; Arithmetic operations
ADD R3, R1, R2 ; R3 = 50 + 12 = 62
SUB R3, R3, #4 ; R3 = 62 - 4 = 58
MUL R4, R3, R2 ; R4 = 58 * 12 = 696
; Logical operations
AND R5, R4, #0xFF ; R5 = 696 & 0xFF = 0xB8 (184)
ORR R5, R5, #0x01 ; R5 = 0xB8 | 0x01 = 0xB9 (185)
BIC R5, R5, #0x08 ; R5 = 0xB9 & ~0x08 = 0xB1 (177)
EOR R5, R5, #0x02 ; R5 = 0xB1 ^ 0x02 = 0xB3 (179)
; Shift and Barrel Shifter
LSL R8, R4, #4 ; logical left shift by 4 bits
LSR R9, R4, #8 ; logical right shift by 8 bits
ADD R5, R5, R5, LSL #1 ; R5 = R5 + (R5 << 1) = 3 * R5
; Store result in memory using a pointer
LDR R6, RP ; R6 = address of RESULT
STR R5, [R6] ; RESULT = R5
; Read back for verification
LDR R7, [R6] ; R7 = RESULT
STOP B STOP
AREA CONSTANTS, DATA, READONLY
NUM1 DCD 50 ; First integer
NUM2 DCD 12 ; Second integer
RP DCD RESULT ; Pointer to RESULT variable
AREA MYDATA, DATA, READWRITE
RESULT SPACE 4 ; Will hold the final computed value
ENDExample 2: Load/Store with Different Addressing Modes
This example demonstrates load and store instructions using various addressing modes.
AREA RESET, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20001000 ; Initial SP (example)
DCD Reset_Handler ; Reset vector
AREA MYCODE, CODE, READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
LDR R0, =ARRAY ; R0 = &ARRAY (base address)
; 1) Immediate Offset: EA = R0 + #8 (third element)
; ---------------------------------------------------------
LDR R1, [R0, #8] ; R1 = ARRAY[2] = ? (expect 30)
LDR R7, =OUT
STR R1, [R7, #0] ; OUT[0] = R1
; 2) Pre-indexed: R2 = [R2 + #4], then R2 = R2 + #4
; Use a scratch pointer so R0 remains the base.
; ---------------------------------------------------------
MOV R2, R0 ; R2 = &ARRAY
LDR R3, [R2, #4]! ; R2 -> &ARRAY[1], R3 = ARRAY[1] = ? (expect 20)
STR R3, [R7, #4] ; OUT[1] = R3
; After this, R2 now points at ARRAY[1].
; 3) Post-indexed: R4 = [R4], then R4 = R4 + #12
; Load first element, then advance pointer to the 4th.
; ---------------------------------------------------------
MOV R4, R0 ; R4 = &ARRAY
LDR R5, [R4], #12 ; R5 = ARRAY[0] = ? (expect 10), R4 -> &ARRAY[3]
STR R5, [R7, #8] ; OUT[2] = R5
; 4) Register Offset: EA = R0 + R6
; Offset register holds byte offset (multiple of 4 for words).
; ---------------------------------------------------------
MOV R6, #12 ; byte offset to ARRAY[3]
LDR R8, [R0, R6] ; R8 = ARRAY[3] = ? (expect 40)
STR R8, [R7, #12] ; OUT[3] = R8
; read second element via register offset
MOV R6, #4 ; byte offset to ARRAY[1]
LDR R9, [R0, R6] ; R9 = ARRAY[1] = ? (expect 20)
STOP B STOP
AREA CONSTS, DATA, READONLY
ARRAY DCD 10, 20, 30, 40 ; four words at consecutive addresses
AREA MYDATA, DATA, READWRITE
OUT SPACE 16 ; capture buffer for observed loads.
ENDTasks
Task 1: Bitwise Register Manipulation
Start with R0 = 0x12345678. Perform the following operations and observe the results in the debugger (verify in hex and binary):
- Clear bits 4–7 (second hex nibble).
- Set bits 8–11 (force that nibble to
F). - Toggle bits 28–31 (highest nibble).
Hint: Use BIC, ORR, and EOR with appropriate masks.
Task 2: Temperature Conversion
Convert between Fahrenheit and Celsius using values stored in memory:
Start from the following data definitions:
AREA CONSTANTS, DATA, READONLY
TEMP_F DCD 70 ; input temperature in Fahrenheit
TEMP_C DCD 22 ; input temperature in Celsius
AREA MYDATA, DATA, READWRITE
OUT_C SPACE 4 ; TEMP_F converted to Celsius
OUT_F SPACE 4 ; TEMP_C converted to Fahrenheit- Load both inputs from memory into registers.
- Compute both conversions using
SUB,MUL,SDIV, andADD. - Store the results in
OUT_CandOUT_F, and verify them in the Memory window. - Change
TEMP_Fto-40and run again (the result should also be −40). Explain why the program must useSDIVrather thanUDIV.
Hint: Integer division truncates, so multiply before you divide. For