PIC16F88x Machine Code, 6 words (84 bits)
A function that expects a
to be at address 0x70 in memory, and b
to be in register W. Returns the result in register W.
The PIC16 uses 14-bit instruction words. Because the number of words is even, this function can be represented exactly in hex with no padding:
0bc1d03d001ff0dfff401
In mpasm syntax, that is:
SUBWF 0x70,F ; Subtract W from the value at 0x70. Leave the result at
; 0x70.
BTFSC STATUS,2 ; Check the zero flag (bit 2 of the STATUS register) and skip
; the next instruction if it's not set.
RETLW 0x00 ; Set the W register to 0 and return.
BTFSC 0x70,7 ; Skip the next instruction if the MSb of the value at 0x70
; is 0 (i.e. the result of subtraction is positive).
RETLW 0xFF ; Set the W register to 0xff (-1) and return.
RETLW 0x01 ; Set the W register to 1 and return.
Bit for bit, this is encoded as:
00 0010 1111 0000
00 0010 SUBWF
111 0000 0x70
1 F
01 1101 0000 0011
01 11 BTFSC
000 0011 STATUS
01 0 2
11 0100 0000 0000
11 01 RETLW
0000 0000 0x00
00 (ignored)
01 1111 1111 0000
01 11 BTFSC
111 0000 0x70
11 1 7
11 0111 1111 1111
11 01 RETLW
1111 1111 0xff
11 (ignored)
11 0100 0000 0001
11 01 RETLW
0000 0001 0x01
00 (ignored)
The PIC16F88x family of microcontrollers have four memory banks of 128 bytes each. (These are actually bytes this time, and not 14-bit words.) Which one is used depends on the value of the STATUS register. However, addresses 0x70 through 0x7f are the same on all banks, so by placing the operands there the function doesn't have to change banks to access them. I'm not aware of any standard calling convention as far as argument location goes, but the return value being in register W is standard (hence the existence of the RETLW
instruction).
a - b
will yield the arithmetic result, even whena
is the lowest representable value andb
is the highest?) \$\endgroup\$