Assembler Simulation

	This is intended to show what an assembler does, and how native code executes,
	in a simplified way.

	The idea is to
	1. Write some source code (or copy and paste it) on the left. See examples below
	2. Click 'Assemble' to see the equivalent native code in the code segment, values
	set up in the data segment if needed, and a symbol.
	3. Click 'Step' to execute one instruction at a time. See the changing instruction
	pointer in the PC register, and the instruction fetched into the IR register.

	In the source code text area it is not possible to use Tab. That will produce 
	the default behaviour of moving to the focus to the next item. That could have
	been changed using JavaScript, but we did not, since users with sight problems
	rely on it.
	
	After exploring this, look at this for notes on using a real assembler on an x86 in Linux.

	
	Memory and instructions
	+++++++++++++++++++++++


	Memory is organised into a code segment, a data segment and a stack. Each memory 
	location is a 'word', and holds a signed integer, displayed in decimal. Real
	memory has 1 byte per address so holds 0000 0000 to 1111 1111.

	Every instruction is 3 words long - an opcode and up to 2 operands
	Instructions with 0 or 1 operands have these replaced by zero
	The opcode is faceted, with 2 digits for the op, followed by 2 single digits, 
	encoding the addressing mode for each operand
	Registers have an 'address'
	A 1, B 2, C 3, D 4, X 5
	A B C D are general purpose registers, X is the index register.
	Register names only upper case, everywhere
	Addressing mode formats
	immediate # 	1 - so #12 means the actual value 12
	indexed ( )		2 - eg (12) means contents of address (12 + X register)
	indirect [ ]	3 - [12] means the contents of address pointed at by location 12
	direct !			4 - !12 means the contents of address 12 
	register %		5 - so %A means register A
	none 0					- eg for a return instruction
	op-codes are:
	mov 01 - move data. mov x y means copy from x to y
	add 02 - add sub mul has result in A register
	sub 03
	mul 04
	div 05 - quotient in A, remainder in B
	cmp 06 - compare, set flags as if for a subtract
	jmp 07 - jump = goto
	brz 08 - branch on zero - an if
	brp 09 - branch on plus
	push 10 - push onto stack
	pop 11 - pop off stack
	call 12 - call subroutine
	ret 13 - return from subroutine
	hlt 14 - halt (real processors do not halt )
	inc 15 - increment - add 1
	brn 16 - branch if not equal
	
	eg
	mov #6, %a : move 5 into the a register
	01 1 5 6 1
	call !50 : call subroutine at address 50
	12 4 0 50 0
	hlt : halt
	14 0 0 0 0
	
	The #data marks the end of code, and the start of the data segment, initialising
	values and creating labels. So source code data should be: 
	instructions
	#data
	data initialisation

	// is used for comments to the end of the line
	
	Addressing modes
	++++++++++++++++
	
	mov #4 %A // immediate and register
	// move 4 into the A register
	
	mov !4 %A // direct
	hlt
	#data
	1
	2
	3
	99
	// move contents of address 4 into A
	
	mov [4] %A // indirect
	hlt
	#data
	1
	98
	3
	2
	// move contents of location pointed at by location 4
	// into A
	
	mov #2 %X
	mov (4) %B // indexed
	hlt
	#data
	1
	98
	88
	1
	// move contents of address pointed to by contents
	// of 4 (1) + X =3, so put 88 into 
	
	Instruction types - arithmetic
	++++++++++++++++++++++++++++++
	
	add #2 #3 // 5 in register A
	sub #1 #3 // -2
	mul #4 #5 // 20
	div #23 #4 // 5 in register A, 3 in register B
	
	Jump
	++++
	
					mov #2 %A
					jmp next
					mov #3 %A // skip this
	next:   mov #4 %A
	
	Compare and branch
	++++++++++++++++++
	
	cmp #5 #2 // compare 5 and 2
	brp skip // branch on plus to skip label
	mov #5 %A
	hlt
	skip: mov #6 %A
	
	Call and return
	+++++++++++++++
	
					mov #2 %A // set parameters
					mov #3 %B
					call subAdd
					hlt
	subAdd: add %A %B
					ret
	
	Loops
	+++++
	
				mov #5 %A // initialise
	loop: inc %A
				cmp %A #8 // compare 8
				brn loop  // branch on not equal to loop label
	// so A goes 5  6 7 8

	Push and pop
	++++++++++++

mov #2 %A
mov #3 %B
push %A // copy register A onto the stack
pop %B  // remove top of stack and place in B