150 likes | 289 Views
Week 12. Assembly Language. Assembly language programs manipulates the resources of the CPU Registers, ALU and memory Keeping track of these resources and their various states of usage is a keep aspect of this type of programming
E N D
Assembly Language • Assembly language programs manipulates the resources of the CPU • Registers, ALU and memory • Keeping track of these resources and their various states of usage is a keep aspect of this type of programming • Assembler accepts the source code and produces the resulting machine code • This is an unambiguous operation, with a one-to-one relationship
Assembly Code • Each line of machine code can have up to four fields label: opcode/assdir operand(s) comment • Label field is optional • Represents the symbolic address of the instruction (or data) that follows it • Used by the assembler in generating machine code for jump instructions or locations of data. • Opcode/assdir field contains • Opcode of instruction or assembler directive • Opcodes (with operands) are translated in machine code • Assembler directives are commands (directives) to direct the assembler to do something
Assembly Code • Operand(s) • Information required by instruction • Instruction specific • Includes immediate values registers, memory addresses, indirect address, symbolic constants, expressions • Comments • Ignored by assembler, but important means of documentation
Org 0x0048 MOV A,#0 JNB 0x93,Act CLR 0x55 Act: MOV R1,0x45 JMP Fin CLR 0x56 Fin: CLR C Example shows example program with assembler directives, label and instructions The org directive does not produce any code, but tells the assembler that what follows it, starts at address 0x48. This is clearly very important in absolute jump and call instructions Assembly Code
Assemblers • Types of assemblers • Macro assemblers • Two-pass asemblers • Native assemmblers • Cross assemblers • Assembler outputs • Hex output • Widely used format. ASCII file. Used in programming actual devices • Lst output • Contains source and assembled code and symbol tables
Assembler Directives • Command interpreted by assembler that causes the assember to perform specific action • Several such commands • Control placement of code • Define symbolic values • Reserve and initialise storage • Does not result in the generation of machine code • With the exception of a few assembler directives, they have no effect on code memory • DB, DD, DS
Common directives • ORG • Used for address control • Example ORG 0x0040 • Used to alter the internal counter used by the assembler to keep track of instruction addresses • END • Indicates end of program
Common directives • DB • Used to initialise code memory Values: DB ‘1’,’gooo’,’0’,LOW(Values) • Causes 4 bytes to be initialised • Comma separated list of expressions follows DB • Other forms include DD and DW
Common directives • EQU • Allows the creation of symbolic constants for the program • Makes programs more readable • Symbols can be used anywhere in program • Addresses, constants, operands or within expressions
Common directives • Absolute segment directives • CSEG,DSEG,XSEG,ISEG and DSEG • Allows the absolute locating of data or code at fixed locations • Relocatable segment directives • SEGMENT • Allows specification of relocatable program section
Lookup table example • Note 8051 does not have efficient means of doing this! MOV A, #0x0A CALL GETVAL …… GETVAL: ; subroutine to index table ; with contents of A PUSH ACC MOV A, #0Ah MOV DPTR, #TABLE MOVC A, @A+DPTR POP ACC RET TABLE: DB '0','1','2','3','4','5','6','7' DB '8','9','A','B','C','D','E','F'
Absolute Assembler • Machine code is located absoluted following assembly • Fine for small dedicated tasks • Restrictive • Code located in one file • Cannot not support creation of code library • Does not support loading of code to different memory space • This is what we’ve worked with this semester
Relocatable Assember • Source file does not have to be complete • Does not have to contain location information or ORG statements • Where missing locations occur (e.g. jump address), a separate table of missing info is appended to the resulting object file. • Do not create hex file, since all information is not there • Separate program called a linker combines object file(s) and library code to produce an executable • Linker also given info about locations for code and data • For systems using OS, there is also the added operation of loading executable
Register banking • 4 register banks exist • Each has 8 registers named R0-R7 • Active bank is determined by two bits (I.e. 4 possibilities) in the PSW • Used for ISRs to allow fast task switching