590 likes | 879 Views
Assemblers. Goals. Assembler is a symbolic notation for machine language It improves readability: Assembler: Move R0,SUM Machine code: 0010 1101 1001 0001 (16 bits). Why assembler ?. Speed of programs in critical applications Access to all hardware resources of the machine
E N D
Goals • Assembler is a symbolic notation for machine language • It improves readability: • Assembler: Move R0,SUM • Machine code: 0010 1101 1001 0001(16 bits)
Why assembler ? • Speed of programs in critical applications • Access to all hardware resources of the machine • Target for compilers
Case • Universele Brander Automaat • Betronic
Machine Language (1) • Is Machine language difficult to learn? • That holds for every unknown language. Machine language is more difficult because you have to work with the specifically defined micro instruction set. • Is Machine language difficult to read and to understand? • Of course; if you do not know the language, but more difficult as an HLL.
Machine Language (2) • Is Machine language difficult to debug? • Often debuggers give the HLL and machine language and the error is only found in the generated machine language! • Is Machine language difficult to maintain? • Maintainable programs are not specifically dependent on the language they are written in, but more on the way they are contructed
Machine Language (3) • Is Machine language difficult to write? • Often HLL languages use libraries to make programming more simple. Machine language programmers often start from scratch. • Machine languageprogramming is time consuming • One estimates that the time for coding a program is only 30% of the total development time.
Machine Language (4) • Compilers make Machine language superfluous • A good machine language program often looks very different from a compiler generated program. Generally a C program will win over an hand made assembly program
Case Universele Brander Automaat • Klant: Nefit Fasto B.V. • Markt: HVAC • Ontwikkelen (1990) en produceren (100k/jaar) van de UBA universele brander- automaat voor Nefit-Fasto voorzien van een bipolaire ASIC. • Eerste product met een universeel karakter, die een fail-safe approval heeft
Case UBA Ignition 230V , Pump and Fan 6 schakel ingangen 8 analoge ingangen 3 schakeluitgangen 3 modulerende uitgangen 2 draads communicatie bus Externe KIM module aansluiting met 178 bytes config settings Asic and micro Computer
UBA software opbouw HWIO Application C- language 15 Kbyte 1 Kbyte
UBA micro computer HWIO MC68HC05B16 24 I/O bi-directional 8 A/D analogue inputs 2 TCAP input timers 2 TCMP output compare 2 PWM D/A outputs 1 SCI serial output 1 COP watchdog 256 bytes RAM 256 bytes EEPROM 16 Kbytes (EP)ROM 1 Kbytes
UBA Pup After Power up Reset special routine - all instruction set in testroutine - 16 bits CRC (99,98%) - Walking A0 and 05 Ramtest - Check on A/D - Mainloop partitioned in modules - Module check in each phase - Acknowlegde module check by pulse to Asic (350mSec) - Interrupt program termination check by pulse to Asic (20mSec) - Communicate through Objects with C-shell
UBA Assembly • Check instruction set • Test of each opcode over and over again • Emergency stop at fault detection • Not possible in “C” • Check on memory • As part of the program • Emergency stop at fault detection • Difficult in “C” • Better control on application • Compiler generated code must be checked on correctness.
Assembler Statements • Executable statements • are translated to real machine instructions (often one-to-one) • Declarations • no code generation • memory reservation • symbolic data declarations • where to start the code execution
Data declarations Label operation operand S EQU 200 ORIGIN 201 N DATA 300 N1 RESERVE 300 ORIGIN 100
Program Addr operation operand START Move N,R1 Move #N1,R2 Clear R0 LOOP Add (R2),R0 Incr R2 Decr R1 Branch>0 LOOP Move R0,S Return End START
Memory lay-out S Move N,R1 100 200 N ..... 300 101 201 ..... ..... 102 202 N1 ..... ..... 103 203 ..... ..... 104 ..... ..... 105 Branch >0 106 107 501 Nn
type source program Source in ASCII editor translate assembler listing Source and Object code +error messages object code machine 1 machine 2 link/load linker/loader run input/ output memory image Flow
Structure assembler(1) • Assembler is hardly more than substitution • substitute 0001 for Move • substitute 0000 0000 0000 0101for#5 • Is level above machine language • Assembler languages for different architectures are alike
Structure assembler(2) Assembler programs contain three kind of quantities: • Absolute: • opcodes, contants: can be directly translated • Relative: • addresses of instructions which are dependent of final memory location • Extern: • call to subroutines
Structure assembler(3) • Literals: constants in programs • Some assemblers act as if literals are immediate operands • Example: Load #1 is equivalent to: Load One ... One: 1
Number notation • Numbers can be represented using various formats: ADD #93,R1 or ADD #%01011101,R1 or ADD #$5D,R1
Stack registers CPU SP PC Main Memory
70 300 20 10 60 Stack operations 0 1 SP
Push Subtract #4,SP Move R0,(SP) 0 SP 1 80 70 300 or: Move R0,-(SP) 20 10 60 80 R0
Pop 0 Move (SP),R0 Add #4,SP 1 80 70 SP 300 or: Move (SP)+,R0 20 10 60 70 R0
Subroutines • More structure in programs • Mimics procedure and function calls in high level programming languages
Calling mechanism Call SUB 200 next instr. 204 PC Link ................ 204 1000 ................ PC Link RTS 204
Question Is Link register sufficient ?
Subroutine nesting • For nesting of subroutines return address in link register must be stored • Can be done by using stacks
Subroutine stack Stack PC Link 204 subroutine Move Link, -(SP) ...... Move (SP)+, Link RTS
Parameter passing(1) • Through registers • fast • limited number of parameters • caller and callee must know where parameters are placed • Example: Move A,R0 Sub: Move R0,C Call Sub ...... RTS
Parameter passing(2) • Through memory • very flexible • slower than through registers • Often implemented through Stack Pointer • Parameters are pushed on stack before calling subroutine • Results are popped from stack after return • Subroutine needs registers
Parameter passing(3) Stack calling subroutine Move #List, -(SP) Move N, -(SP) Call LISTADD Move 4(SP), SUM Add #8, SP SP
Parameter passing(3) Stack calling subroutine Move #List, -(SP) Move N, -(SP) Call LISTADD Move 4(SP), SUM Add #8, SP SP LIST
Parameter passing(3) Stack calling subroutine Move #List, -(SP) Move N, -(SP) Call LISTADD Move 4(SP), SUM Add #8, SP n SP LIST
Parameter passing(3) Stack calling subroutine Move #List, -(SP) Move N, -(SP) Call LISTADD Move 4(SP), SUM Add #8, SP Return SP n LIST
Parameter passing(3) Stack calling subroutine Move #List, -(SP) Move N, -(SP) Call LISTADD Move 4(SP), SUM Add #8, SP n SP sum
Parameter passing(3) Stack calling subroutine Move #List, -(SP) Move N, -(SP) Call LISTADD Move 4(SP), SUM Add #8, SP SP
Parameter passing(4) LISTADD Move R0, -(SP) ... Move 16(SP), R1 Move 20(SP), R2 Clear R0 LOOP Add (R2), R0 Decr R1 Incr R2 Branch>0 LOOP Move R0, 20(SP) Move (SP)+, R2 ..... Return Stack frame [R0] SP Return n LIST Subroutine
Parameter passing(4) LISTADD Move R0, -(SP) ... Move 16(SP), R1 Move 20(SP), R2 Clear R0 LOOP Add (R2), R0 Decr R1 Incr R2 Branch>0 LOOP Move R0, 20(SP) Move (SP)+, R2 ..... Return Stack frame [R2] SP [R1] [R0] Return n LIST Subroutine
Parameter passing(4) LISTADD Move R0, -(SP) ... Move 16(SP), R1 Move 20(SP), R2 Clear R0 LOOP Add (R2), R0 Decr R1 Incr R2 Branch>0 LOOP Move R0, 20(SP) Move (SP)+, R2 ..... Return Stack frame [R2] SP [R1] [R0] Return n sum Subroutine
Parameter passing(4) LISTADD Move R0, -(SP) ... Move 16(SP), R1 Move 20(SP), R2 Clear R0 LOOP Add (R2), R0 Decr R1 Incr R2 Branch>0 LOOP Move R0, 20(SP) Move (SP)+, R2 ..... Return Stack frame [R1] SP [R0] Return n sum Subroutine
Parameter passing(4) LISTADD Move R0, -(SP) ... Move 16(SP), R1 Move 20(SP), R2 Clear R0 LOOP Add (R2), R0 Decr R1 Incr R2 Branch>0 LOOP Move R0, 20(SP) Move (SP)+, R2 ..... Return Stack frame Return SP n sum Subroutine
Frame Pointer Access variables Through Index Addressing SP saved [R1] (stack pointer) saved [R0] localvar3 localvar2 localvar1 Stack FP frame saved [FP] (frame pointer) for called Return address subroutine param1 param2 param3 param4 Old TOS (top-of-stack)
Re-entrancy • Subroutines can be called more than once • Recursion: subroutine calls itself • Sub A calls Sub B, which in turn calls Sub A • Multiple callers “at the same time” • Special measures for re-entrancy • No change of instructions • Each caller must have its own copy of data • Use stack(s)
Additional Instructions • Logic instructions • Not R0; invert all bits in R0 • And #$FF000000,R0; AND with bit string • Shift and rotate instructions • Many variants for different purposes
Logical shifts C R0 0 e.g. used in Packing . . . before: 0 0 1 1 1 0 0 1 1 . . . after: 1 1 1 0 0 1 1 0 0 (a) Logical shift left LShiftL #2,R0 0 R0 C . . . 0 1 1 1 0 0 1 1 before: 0 . . . after: 0 0 0 1 1 1 0 0 1 (b) Logical shift right LShiftR #2,R0
Arithmetic shifts R0 C . . . before: 0 1 0 0 1 1 0 1 0 . . . after: 1 1 1 0 0 1 1 0 1 (c) Arithmetic shift right AShiftR #2,R0