Instructions

z80 » Beginner

Let's learn some basic instructions you can use in your programs like this jumbled mess!
start:			;label 'start' given the
			; address of the following
			; byte in memory (in
			; this case the 'ld a,__'
			;comments
			; comments
		;comments
 ;comments
;comments....more comments;;;;more comments
	;the assembler doesn't even see this
	ld a,23		;store 23 in a
	ld b,45		;store 45 in b
	add a,b		;23+45=68 stored in a

	ld hl,$fc00	;store $fc00 in hl
	ld de,23	;store 23 in de
	add hl,de	;$fc00+23=$fc17

	call _clrLCD	;clear the screen

	cp b		;is a=b?
	cp 3		;is a=3?

	ld b,4
loop:
	call _clrLCD	;clear screen 4 times
	djnz loop	;b-1 stored in b
			;if b=0...we're done
	ret		;return from where we
			; came from

This is a listing of the most commonly used assembler instructions. Depending upon your type of assembler, you may need to insert white-space (leading spaces or tabs) before instructions. Labels and comments do not require white-space.

labels

arg1: - The colon after a non-spaced, 31 or less name is a label. The assembler uses it as a reference point for calls and jumps. It can contain underscores (_). Labels are given the value of the address of the following byte of code. They take up zero space in the program so you can have as many labels as you want. You need to put no spaces between the left margin of the source code and the label name, or the assembler won't recognize it.
label:
boring_label:
a:
run_to_me_now:
this_label_is_too_long_to_be_a_label:

comments

; comments - The semi-colon tells the processor "Ignore the rest of this line". It is useful for adding little reminders as to what code may do so that later readers understand complex routines.
	ld bc,$0303 ;load b with 3, c with 3
	;this is all ignored

ld

ld arg1, arg2 - Copies arg2 into arg1. You can either copy a register's value into an address or an address into a register's value, but you can not copy a value directly into a memory address, the value needs to go through a register first. It's like when you eat: the food can't go directly from your plate to your stomach, it has to go through your mouth (that was cheesy).
	ld hl,(256*2)+1		;same as $0201
	ld b,$3
	ld a,%01101001
	ld a,%010		;same as %00000010
	ld (hl),$35		;load byte at address
				; hl
	ld (de),a		;ld into address at de
	ld ($80df),hl
	ld ($fc00),a		;load a into first
				; byte of video memory
	ld a,b
	ld a,($8100)

	ld ($8309),%10011100	;CAN'T do this!
	ld (_textShadow),$a4	;CAN'T do this!

call

call addr1 - Processor goes to the specified address (addr1) and starts executing code until it is told to go back. There are two types of calls: a ROM call and an address call. ROM calls go to an area in the TI86's own memory whereas an address call will jump to an address in your program or in the asm program. Most frequent ROM calls and asm calls are listed in ti86asm.inc. They are listed as aliases to addresses in the TI86's memory. ROM calls and address calls are not different. I just made the distinction to show you that some people refer to calls made to an address in TI-OS's own memory instead of the actual program running.
	call _clrLCD
	call $4a7e	;$4a7e is address of
			; _clrLCD
	call my_routine	;routine somewhere in your
			; program
Call's made inside the program are called absolute calls because they require the exact, absolute, position of the label in the memory. Calls made to parts of the program itself are absolute also. At the start of almost all your programs will be the line of code .org _asm_exec_ram which was an alias saying .org $d748. This tells the assembler to make all the label referencing while considering that the program is at address $d748. The processor encounters call my_routine, which is inside the program. It then takes the address of the my_routine label, for example $0034 and adds that to $d748 to get $d77c. Then it returns to where it encountered call my_routine and substitutes that with call $d77c. When it encounters call _clrLCD, which it knows to be an alias, it just substitutes the alias for call $4a7e. Can be used with flags and conditions.

cp

cp arg1 - Compares whatever is in the a register to arg1. Arg1 can be either a value, a register that points to an address, or another register. Modifies the processor flags accordingly.
	cp h		;compare h to a
	cp $03
	cp 250
	cp %00000111
	cp (hl)

add

add arg1, arg2 - Adds the value of arg2 to the value of arg1 and stores the result into arg1. Arg1 and arg2 can be either a register pointing to an address and a register, a register and a register, or a register and a value. Arg2 does not change.
	add a,(hl)	;add contents of memory at
			; address hl with a and store
			; result in a
	add hl,de
	add a,b
	add a,$23

sub

sub arg1 - Subtracts the value of arg1, or the register to which it is refering, from a and stores the result in a. Arg1 does not change.
	sub 32
	sub (hl)
	sub c

inc

inc arg1 - Increases the value of, or address pointed to by, arg1 by one. Arg1 can be a register or a pointer to a register.
	inc (hl)	;increase byte at address hl
	inc a
	inc bc
	inc e

dec

dec arg1 - Decreases the value, or address pointed to by arg1, by one. Arg1 can be a register or a pointer to a register.
	dec (hl)	;decrease byte at address hl
	dec a
	dec b
	dec de

djnz

djnz addr1 - Similar to a "For()" loop in TI-BASIC, djnz stands for "Decrease register b by one and Jump if Not Zero to addr1". This decreases the value of register b by one and jumps to start executing code at addr1 if b is not zero yet, but if b has hit zero, it will just continue on in the code.
djnz LabelToSomewhere
Here it is in context with other code. This is just to give you the basic idea of its use:
	ld b,10		;number of iterations in
			; loop
BeginningOfForLoop:
	. . .		;code to be executed in
			; For() loop
	djnz BeginningOfForLoop
	. . .		;code to be executed once
			; For() loop is done

jr

jr addr1 - Jumps to addr1 and starts executing code. Addr1 must be within about 120 bytes from the jr addr1 instruction. Assembly Studio should give you an error if the address is too far away.
	jr Label_To_Somewhere_Before_This_Command
	jr Label_To_Somewhere_After_This_Command
Relative jumps move to an address relative to where the processor is executing code. The jump may tell the processor to "move to the label that is 35 bytes backwards from where you are now". That is why they are relative to the current position of where the processor is executing code. Can be used with flags and conditions.

jp

jp addr1 - Jumps to addr1 and starts executing code. Addr1 can be any distance from the jp addr1 instruction. It is best to use jr addr1 instead wherever possible because jp addr1 takes up one byte more byte in memory.
	jp $01be		;jump to address $01be
	jp LabelToSomewhere
Absolute jumps made inside the program are called absolute calls because they require the exact, absolute, position of the label in the memory distance wise from zero, or the beginning of the memory at $0000. Can be used with flags and conditions.

ret

ret - Used with call addr1. Tells the processor to go back to wherever it was called from and continue executing the code following the call. Can be used with flags and conditions.
	ret


More from z80 » Beginner
Aliases // Convert from decimal to hexadecimal or binary // Flags and Conditions // Format and Compiling // Instructions // Math // Number Bases // Oh, No! It Crashed! // Registers // TI-BASIC to Asm Comparisons // TI86 Specifications // Two's Compliment // z80 Processor