ARM Assembly and Input/Output

Conditionals in Assembly

Moving imediate values to registers

mov r3, #val ;val can be 0 to 255
mvn r3, #val ;val can be -256 to -1
ldr r3, =val ;val can be any 32-bit value

ldr is a pseudo-op!

Conditionals in assembly

int a = 1;
int b = 2;
int x;

if (a < b) {
  x = 1;
} else {
  x = 2;
}
.section .text
  ldr r0, =vars
  ldr r1, [r0]
  ldr r2, [r0, #4]
  cmp r1, r2
  bge else
  mov r2, #1
  b endif
else:
  mov r2, #2
endif:
  str r2, [r0, #8]

.section .data
vars:
  .word 1 ;a
  .word 2 ;b
  .word 0 ;x

Loops in assembly

Basic example

int a = 1;
int b = 2;

while (a != b) {
  // loop body
}
.section .text
  ldr r0, =vars
  ldr r1, [r0]
  ldr r2, [r0, #4]
loop:
  cmp r1, r2
  beq end
  ; loop body
  b loop
end:

.section -data
vars:
  .word 1 ;a
  .word 2 ;b

Another example

int a = 1;
int b = 2;

while (a && b) {
  // loop body
}
.section .text
  ldr r0, =vars
  ldr r1, [r0]
  ldr r2, [r0, #4]
loop:
  cmp r1, #0
  beq end
  cmp r2, #0
  beq end
  ; loop body
  b loop
end:

.section -data
vars:
  .word 1 ;a
  .word 2 ;b

Function Calls in Assembly

Function/procedure/method calls

ARM support for function calls

Example of function call in assembly

int add(int a, int b) {
  return a + b;
}

main() {
  int x = 5; y = 10;
  add(x, y);
}
add:
  add r0, r0, r1
  mov r15, r14

main:
  mov r0, #5
  mov r1, #10
  bl add

ARM register convention on function calls

Nested Function Calls in Assembly

int addNum (int a, int b) {
  return a + b;
}

int add (int a, int b) {
  return addNum(a, b);
}

main() {
  int x = 5, y = 10;
  add(a, b);
}
addNum:
  add r0, r0, r1
  mov r15, r14

add:
  bl addNum
  mov r15, r14 ;stuck in infinite loop!

main:
  mov r0, #5
  mov r1, #10
  bl add

Stack

Nested function calls using stack

addNum:
  add r0, r0, r1
  mov r15, r14

add:
  push {r14}
  bl addNum
  pop {r15}

main:
  push {r14}
  mov r0, #5
  mov r1, #10
  bl add
  pop {r15}

Other uses of stack

RISC vs CISC

Should an ISA be simple or complex?

CISC vs RISC ISA

Input/Output Devices

I/O Devices

Device Behaviour Partner Data Rate (Mbit/sec)
Keyboard Input Human 0.001
Mouse Input Human 0.004
Voice input Input Human 0.26
Laser printer Output Human 3.2
Graphics Output Human 800-8,000
Magnetic disk Storage Machine 800-3,000
Network/LAN Input or output Machine 100-40,000 (40Gbit/sec)

Connecting I/O devices to CPU

Connecting multiple I/O devices: I/O Controllers

Addressing I/O devices

A processor can support both modes

Memory mapped I/O example Thumb

DEVICE_BASE = 0x1000
REG1 = 0;
REG2 = 4;
LDR r1, =DEVICE_BASE
LDR r0, [r1, #REG1] // read from reg1
MOV r0, #8
STR r0, [r1, #REG2] // write to reg2

Accessing I/O Device

How to Check I/O status: Is it ready?

Interrupt Interface

What happens on an interrupt?

Most of the functionality is similar to procedure call

Connecting multiple I/O devices

What if there are more devices than interrupt ports on CPU?

CPU needs to poll to check which device triggered the interrupt

Interrupt Controller arbitrates CPU access among devices

Handling multiple devices

How to find interrupt handlers of a device?

What happens if two devices request interrupt at the same time?

Which interrupt to handle first?

Summary of interrupt handling

Exceptions and Traps

Interrupts/Exceptions/ Traps

CPU Buses

Double meaning

Bus Signals

How to communicate over bus

Four stage handshake

  1. Device 1 raises enquiry signal
  2. Device 2 acknowledges, Data transfer happens
  3. One of the devices lowers its signal
  4. Other device terminates operation

Asynchronous bus read/write

Asynchronous bus: Events can happen at any time.

Synchronous bus read/write

Synchronous bus: Events are synchronized with a clock signal.

Wait States

Slow devices are not able to provide data at next clock cycle

Burst Transfer

Transfers multiple words in a single request

Direct Memory Access

Parallel computation and data transfer

What does CPU do during DMA?

Bus Hierarchy