How Stuff Works: Processors Principles and Examples Joe Finney joe@comp.lancs.ac.uk
Aims Today we re going to Discuss the purpose for Assembly Language Review the common principles on which Assembly Languages are based Study a REAL assembly language Read and write simple assembly programs
Computer Processors Execute machine code instructions. Every clock tick, the next instruction is executed* Machine code is essentially a processor specific, binary representation of a simple instruction. Provide the smallest building block we use to create computer programs. All higher level languages (C, Python, Java ) are ultimately translated one way or another into machine code. Abstract Reality Java / Python / C#... Assembly Language Machine Code Assembly Language is a textual, humanly readable* programming language 1:1 mapping between an ASSEMBLY instruction and a MACHINE CODE instruction.
Why? Sometimes (but rarely) we want to write code at this very low level Programs with extremely tight timing constraints times to execute line of code are deterministic. Abstract Java / Python / C... Assembly Language Machine Code May need access to processor in a way not supported by high level languages. E.g. If you were writing a scheduler. Reality If you were stuck on an alien planet with no software. You could write the first compiler in assembler.
Principles One assembler instruction per line One assembler instruction matches one machine instruction. An assembler instruction has one operator and zero or more comma separated operands. Operators are instructions (things to do) Include instructions like ADD, SUB, GOTO, BZ Defined by the processor s capabilities. e.g. ADD R0, 1 Operands are parameters (data to work on) Use on of four addressing modes.
Addressing Modes Operands can be immediate Operand contains a literal value. Typically denoted by a # ADD R0, #1 Operands can be direct Operand contains memory address of the value Typically the default. ADD R0, 1 Operands can be indexed Operand contains memory address of the value plus and offset Typically uses a + sign ADD R0, 1+16 Operands can be indirect Operand contains the memory address of the memory address that holds the value ADD R0, [1]
Example Worlds smallest commercial 8 bit computer Size is 3mm x 2mm x 0.9 mm Cost is around 30 pence per unit (retail) Weighs considerably less than one gram Capable of one million operations /sec Highly reliable designed to benefit in situ for decades at a time (40 years data retention) Consumes less than 15uW at low speeds
PIC Assembler Syntax PIC Assembler has strict formatting [Label:] [<opcode> [<operand>][, <destination>]] Everything is TAB separated. (one or more) At most one opcode per line (instruction to execute) Depending on the opcode, may also have an operand and a destination. Operand is opcode specific, either a RAM address or literal a value. Destination (if present) is always either W or F. W = Working Register F = File (the one specified in the operand) Let s Review its Instruction set
Write a program to Add together two numbers, which are stored in memory addresses 0x40 and 0x41. The result should be stored in memory location 0x42. MOVF ADDWF MOVWF 0x40, W 0x41, W 0x42 END
Write a program to Determine if the number stored in memory location 0x40 is odd or even. Write the ASCII value for the letter E or O to memory location 0x41 to store the result. MOVLW 69 BTFSC 0x40, 0 MOVLW 79 MOVWF 0x41 END
Write a program to Perform a countdown from the value five to the value one. Write each number to memory location 0x41 in turn. MOVLW 5 MOVWF 0x40 COUNTDOWN: MOVF MOVWF DECFSZ GOTO 0x40, W 0x41 0x40, F COUNTDOWN END
References
References
References
References http://www.microchip.com/mplab/mplab-x-ide