Chapter 7Chapter 7LC-3 Assembly Language2Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyCS RealityCS Reality You’ve got to know assembly Chances are, you’ll never write program in assembly– Compilers are much better & more patient than you are Understanding assembly key to machine-level execution model– Behavior of programs in presence of bugs High-level language model breaks down– Tuning program performance Understanding sources of program inefficiency– Implementing system software Compiler has machine code as target Operating systems must manage process state3Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyIt’s hard to write code in 1’s & 0’s!It’s hard to write code in 1’s & 0’s! Assembly language makes it possible to write Machine Language code– each line of assembly language is translated into a single ML instruction A program called the Assembler does the translation and provides useful tools:– use of labels - symbolic names for address locations– automatic conversion of binary / hex / decimal– pseudo-ops4Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyAssembly Language InstructionsAssembly Language Instructions Formats– LABEL OPCODE OPERANDS ; COMMENTS– LABEL PSEUDO-OPS ; COMMENTS Opcode– Symbolic name for the 4-bit ML opcode Label– Symbolic name for a memory location. It is used to: indicate the target of a branch instruction, e.g. AGAIN in location 0B indicate the location of a stored value or array, e.g. NUMBER and SIX Comments– intended for humans only: explanation of code, visual display5Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyPseudoPseudo--Ops … Ops … – … are directives to the assembler they are not translated into ML instructions– LC-3 Pseudo-Ops: .ORIG address Tells assembler where to locate the program in memory (starting address). .FILL value Store value in the next location in memory. .BLKW n Set aside a block of n words in memory. .STRINGZ string Store the string, one character per word, in memory. Add a word of x0000 after the string. .END Marks the end of the source program (not to be confused with the instruction HALT!) .EXTERNAL The label so indicated is allocated in another module.6Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyA sample programA sample program01 ;02 ; Program to multiply an integer by the number 603 ;04 .ORIG x305005 LD R1, SIX06 LD R2, NUMBER07 AND R3, R3, #0 ; clear R3 to hold the product08 ;09 ; The inner loop0A ;0B AGAIN ADD R3, R3, R20C ADD R1, R1, #-1 ; keep track of iterations0D BRp AGAIN0E ;0F HALT10 ;11 NUMBER .BLKW 112 SIX .FILL x000613 ;14 .END7Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyFrom Assembly to bits…From Assembly to bits….ORIG x3000AND R1, R1, #0ADD R1, R1, #10LD R2, TwentyLD R3, EssHALTTwenty FILL x0014.BLKW 2Ess .FILL “S” .STRINGZ “Hi”.BLKW 3.ENDx3000: AND R1, R1, 0 0000x3001: ADD R1, R1, 0 1010x3002: LD R2, 0 0000 0010x3003: LD R3, 0 0000 0100x3004: TRAP 0010 0101 x3005: 0000 0000 0001 0100 ; x0014x3006:x3007:x3008: 0000 0000 0101 0011 ; x0053 = ‘S’x3009: 0000 0000 0100 1000 ; x0048 = ‘H’x300A: 0000 0000 0110 1001 ; x0069 = ‘i’x300B: 0000 0000 0000 0000 ; x0000 = null terminatorx300C:x300D:x300E:8Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyThe Assembly ProcessThe Assembly Process Objective– Translate the AL (Assembly Language) program into ML (Machine Language).– Each AL instruction yields one ML instruction word.– Interpret pseudo-ops correctly. Problem– An instruction may reference a label.– If the label hasn’t been encountered yet, the assembler can't form the instruction word Solution– Two-pass assembly9Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyTwoTwo--Pass Assembly Pass Assembly -- 11First Pass - generating the symbol table–Scan each line–Keep track of current address Increment by 1 for each instruction Adjust as required for any pseudo-ops(e.g. .FILL or .STRINGZ, etc.)–For each label Enter it into the symbol table Allocate to it the current address–Stop when .END is encountered10Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblySymbol Table example Symbol Table example – Using the earlier example:Symbol AddressAgain x3053Number x3057Six x3058;; Program to multiply a number by six;.ORIG x3050x3050 LD R1, SIXx3051 LD R2, NUMBERx3052 AND R3, R3, #0;; The inner loop;x3053 AGAIN ADD R3, R3, R2x3054 ADD R1, R1, #-1 x3055 BRp AGAIN;x3056 HALT;x3057 NUMBER .BLKW 1x3058 SIX .FILL x0006;.END11Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyTwoTwo--Pass Assembly Pass Assembly -- 22Second Pass - generating the ML program–Scan each line again–Translate each AL instruction into ML Look up symbols in the symbol table instruction Ensure that labels are no more than +256 / -255 lines from instruction Determine operand field for the instruction–Fill memory locations as directed by pseudo-ops–Stop when .END is encountered12Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyAssembled codeAssembled code– Using the earlier example:Symbol AddressAgain x3053Number x3057Six x3058x3050 0010 001 0 0000 0111 ; LD R1, SIXx3051 0010 010 0 0000 0101 ; LD R2, NUMBERx3052 0101 011 011 1 00000 ; AND R3, R3, #0x3053 0001 011 011 0 00 010 ; ADD R3, R3, R2x3054 0001 001 001 1 11111 ; ADD R1, R1, #-1 x3055 0000 001 1 1111 1101 ; BRp AGAINx3056 1111 0000 0010 0101 ; HALTX3057 ; .BLKW 1x3058 0000 0000 0000 0110 ; .FILL x000613Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyAnother exampleAnother example.ORIG x30003000 AND
View Full Document