Download presentation
Presentation is loading. Please wait.
Published byDawson Finkley Modified over 10 years ago
1
1 Patt and Patel Ch. 7 LC-3 Assembly Language
2
2 LC-3 is a load/store RISC architecture Has 8 general registersHas 8 general registers Has a flat 16-bit addressing rangeHas a flat 16-bit addressing range Has a 16-bit word sizeHas a 16-bit word size Load variables from memory to registerLoad variables from memory to register
3
3 Syntax of LC-3 One instruction, declaration per lineOne instruction, declaration per line Comments are anything on a line following “;”Comments are anything on a line following “;” Comments may not span linesComments may not span lines LC-3 has 2 basic data typesLC-3 has 2 basic data types –Integer –Character Both are take 16-bits of space (a word) though a character is only 8-bits in size.Both are take 16-bits of space (a word) though a character is only 8-bits in size.
4
4 Labels Symbolic names that are used to identify memory locationsSymbolic names that are used to identify memory locations Location for target of a branch or jumpLocation for target of a branch or jump Location for a variable for loading and storingLocation for a variable for loading and storing Can be 1-20 characters in sizeCan be 1-20 characters in size LC-3 Syntax
5
5 Directives give information to the assembler. All directives start with ‘.’ (period) LC-3 SyntaxDirectiveDescription.ORIG Where to start in placing things in memory.FILL Declare a memory location.BLKW Reserve a group of memory locations.STRINGZ Declare a group of characters in memory.END Tells assembly where your program source ends
6
6 LC-3 Syntax.ORIG Tells simulator where to put your code in memoryTells simulator where to put your code in memory Only one allowed per programOnly one allowed per program PC gets set to this address at start upPC gets set to this address at start up Similar to the “main” in “C”Similar to the “main” in “C”
7
7 “C” type varname; “LC-3” varname.FILLvalue type is int(integer) char(character) float(floating point) value is required – the initial value LC-3 Syntax
8
8 flag.FILLx0001 counter.FILLx0002 letter.FILLx0041 ; A letters.FILLx4241 ; BA LC-3 Syntax.FILL One declaration per lineOne declaration per line Always declaring 16-bits, the word size of LC-3Always declaring 16-bits, the word size of LC-3
9
9.BLKW Tells assembler to set aside some number of sequential memory locationsTells assembler to set aside some number of sequential memory locations Useful for arraysUseful for arrays Can be initializedCan be initialized LC-3 Syntax
10
10 Examples of.BLKW: ;set aside 3 locations.BLKW3 ;set aside 1 location and label it. Bob.BLKW1 ;set aside 1 location, label and init to 4. Num.BLKW1#4 LC-3 Syntax
11
11.STRINGZ Used to declare a string of charactersUsed to declare a string of characters Is terminated by x0000Is terminated by x0000 Example: hello.STRINGZ“Hello World!” LC-3 Syntax
12
12.END Tells the assembler where your program endsTells the assembler where your program ends Only one per allowed in your programOnly one per allowed in your program LC-3 Syntax
13
13 An immediate is a value specified in an instruction, not by a.FILL declaration “LC-3”“C” LD R1, X LD R2, Y ADD R3, R2, #0 Z = Y ADD R3, R1, R2 Z = X + Y ??? Z = X – Y ??? Z = X * Y ??? Z = X / Y ST R3, Z LC-3 Syntax
14
14.ORIGx3000 LDR2, Zero LDR0, M0 LDR1, M1 LoopBRzDone ADDR2, R2, R0 ADDR1, R1, -1 BRLoop DoneSTR2, Result HALT Result.FILLx0000 Zero.FILLx0000 M0.FILLx0004 M1.FILLx0002 Simple LC-3 program What does this program do?What does this program do? What is in “Result” at the end?What is in “Result” at the end?
15
15 Program Execution Assembler translates to executable – machine languageAssembler translates to executable – machine language Linker combines multiple LC-3 files – if anyLinker combines multiple LC-3 files – if any Loader puts executable into memory and makes the CPU jump to first instruction,.ORIG.Loader puts executable into memory and makes the CPU jump to first instruction,.ORIG. ExecutesExecutes When executing is done returns control to OSWhen executing is done returns control to OS Or simulator or monitorOr simulator or monitor Load again to run again with different dataLoad again to run again with different data In this case, assemble again, too, since data is in program.In this case, assemble again, too, since data is in program.
16
16 if (condition) statement; else statement; LC-3 Programming HLL – if/else statements…
17
17 “LC-3” LD R0, count BRnzendif ADDR0, R0, #1 endif ; next instruction goes here LC-3 Programming “C” if (count < 0) count = count + 1;
18
18 Loops can be built out of IF’s – WHILE: “C” while (count > 0) { a += count; count--; } LC-3 Programming
19
19 “LC-3” LDR1, a LDR0, count while BRnzendwhile ADDR1, R1, R0 ADDR0, R0, #-1 BRwhile endwhile STR1, a STR0, count LC-3 Programming
20
20 Procedure Calls Simple procedure calls require 2 instructions: “RET” Jump Return Be careful with registers!! Cannot nest unless R7 is saved elsewhere Cannot be recursive without a stack “JSR” or “JSRR” Jump Service Routine Saves the return address into R7
21
21 LC-3 Procedures JSRSub; calls procedure … ; calculate R2 = R0-R1 SubNOTR2, R1 ADDR2, R2, #1 ADDR2, R2, R0 RET; returns to line after ; JSR Sub Example
22
22 Repeat loops “C” /* do statement while expression is TRUE */ /* when expression is FALSE, exit loop */ do { if (a < b) a++; if (a > b) a--; } while (a != b) LC-3 Procedures
23
23 “LC-3” LDR0, a LDR1, b JSRSub repeatBRpzsecondif ADDR0, R0, #1 secondifBRnzuntil ADDR0, R0, #-1 untilJSRSub BRnprepeat LC-3 Procedures
24
24 “C” for ( I = 3; I <= 8; I++) { a = a+I;} For loops LC-3 Procedures
25
25 “LC-3” LDR0, a ANDR1, R1, #0; init I to zero ADDR1, R1, #3; now make 3 forADDR2, R1, #-8 BRpendfor ADDR0, R0, R1; a=a+I ADDR1, R1, #1: I++ BRfor endfor LC-3 Procedures
26
26 TRAP (System Calls) Use the “TRAP” instruction and a “trap vector”. Very tedious and dangerous for a programmer to deal with IO. This is why we like to have an OS. Need an instruction though to get its attention.
27
27 Trap Vector Assembler Name Usage & Result 0x20 GETC Read a character from console into R0, not echoed. 0x21 OUT Write character in R0 to console. 0x22 PUTS Write string of characters to console. Start with character at address contained in R0. Stops when 0x0000 is encountered. 0x23 IN Print a prompt to console and read in a single character into R0. Character is echoed. 0x24 PUTSP Write a string of characters to console, 2 characters per address location. Start with characters at address in R0. First [7:0] and then [15:0]. Stops when 0x0000 is encountered. 0x25 HALT Halt execution and print message to console. Trap Service Routines
28
28 To print a character ; address of the char must be in R0. TRAPx21 or OUT To read in a character ; will go into R0, no echo. TRAPx20 or GETC Trap Examples
29
29 To end your program: TRAPx25 TRAPx25orHALT SYS Calls Examples
30
30 Questions?
31
31
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.