ARM7 Division

From BenningtonWiki
Jump to: navigation, search

The Program

#include "gbaio.h"

int main(void) {

	int a, b, result, remainder;

	start_main();

	a = 250;
	b = 13;		// a over b

	result = 0;
	while (a >= b) {
		result += 1;
		a -= b;
	}	
	remainder = a;

	draw_number(0, 0, result);
	draw_number(0, 10, remainder);
	
end:	goto end;
	
}

The Disassembly

   0:	b510      	push	{r4, lr}
   2:	fffef7ff 	bl	start_main
   6:	24fa      	mov	r4, #250
   8:	230d      	mov	r3, #13
   a:	2200      	mov	r2, #0
   c:	3201      	add	r2, #1
   e:	1ae4      	sub	r4, r4, r3
  10:	429c      	cmp	r4, r3
  12:	dafb      	bge	c
  14:	2000      	mov	r0, #0
  16:	2100      	mov	r1, #0
  18:	fffef7ff 	bl	draw_number
  1c:	2000      	mov	r0, #0
  1e:	210a      	mov	r1, #10
  20:	1c22      	mov	r2, r4
  22:	fffef7ff 	bl	draw_number
  26:	e7fe      	b	26

The Bits

1011010100010000
1111111111111110
1111011111111111
0010010011111010
0010001100001101
0010001000000000
0011001000000001
0001101011100100
0100001010011100
1101101011111011
0010000000000000
0010000100000000
1111111111111110
1111011111111111
0010000000000000
0010000100001010
0001110000100010
1111111111111110
1111011111111111
1110011111111110

20 instructions * 16 bits per instruction = 320 bits

Instructions

push {rr, ...}
Encoding: 1011010 L76543210
Pushes the specified registers and optional link register (lr) onto the Stack and decrements the stack pointer (sp) accordingly.

bl function
Encoding: 11111 xxxxxxxxxxx 11110 xxxxxxxxxxx
"Branch and save link register." Calls a function. It "remembers" where to come back to by storing the address of the next instruction (at the program counter + 4) in the link register (lr). This instruction is unusual in that it is 32 bits long, and the address of the function to call is split between the two 16 bit halves.

mov rr, #n
Encoding: 00100 rrr nnnnnnnn
Move a number (expressed in decimal in the disassembly) into register r.

add rr, #n
Encoding: 00110 rrr nnnnnnnn
Add a number (expressed in decimal in the disassembly) to register r.

sub rr, ra, rb
Encoding: 0001101 bbb aaa rrr
Subtract register b from register a and store the result in register r.

cmp rr, ra
Encoding: 0100001010 aaa rrr
Compare register a to register r and set the condition codes as a result.

bge label
Encoding: 11011010 xxxxxxxx
"Branch if greater than or equal." Branches to another address in the program if the N flag is clear or the Z flag is set. The encoding uses an offset (positive or negative number) from the current instruction plus 4 to specify the destination address.

mov rr, ra
Encoding: 0001110000 aaa rrr
Move register a into register r.

b
Encoding: 11100111 xxxxxxxx
Branch to another address in the program. The encoding uses an offset (positive of negative number) from the current instruction plus 4 to specify the destination address.

Character Sheet

File:ARM7 Character Sheet.pdf