LC-3 Assembly Language
description
Transcript of LC-3 Assembly Language
![Page 1: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/1.jpg)
1
Patt and Patel Ch. 7Patt and Patel Ch. 7
LC-3 LC-3 Assembly LanguageAssembly Language
![Page 2: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/2.jpg)
2
LC-3 is a load/store RISC LC-3 is a load/store RISC architecturearchitecture
• 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 Load variables from memory to
registerregister
![Page 3: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/3.jpg)
3
Syntax of LC-3Syntax 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 Both are take 16-bits of space (a word) though a character is only 8-bits in size.a character is only 8-bits in size.
![Page 4: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/4.jpg)
4
LabelsLabels
• Symbolic names that are used to Symbolic names that are used to identify identify memory locationsmemory locations
• Location for target of a branch or Location for target of a branch or jumpjump
• Location for a variable for loading Location for a variable for loading and storingand storing
• Can be 1-20 characters in sizeCan be 1-20 characters in size
LC-3 Syntax
![Page 5: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/5.jpg)
5
Directives give information to the assembler. All directives start with ‘.’ (period)
LC-3 Syntax
DirectiveDirective DescriptionDescription
.ORIG.ORIG Where to start in placing things in Where to start in placing things in memorymemory
.FILL.FILL Declare a memory locationDeclare a memory location
.BLKW.BLKW Reserve a group of memory locationsReserve a group of memory locations
.STRINGZ.STRINGZ Declare a group of characters in Declare a group of characters in memorymemory
.END.END Tells assembly where your program Tells assembly where your program source endssource ends
![Page 6: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/6.jpg)
6
LC-3 Syntax
.ORIG.ORIG
• Tells simulator where to put your Tells simulator where to put your code in memorycode in memory
• Only one allowed per programOnly one allowed per program• PC gets set to this address at start PC gets set to this address at start
upup• Similar to the “main” in “C”Similar to the “main” in “C”
![Page 7: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/7.jpg)
7
“C”type varname;
“LC-3”varname .FILL value
type isint (integer)char (character)float (floating point)
value is required – the initial value
LC-3 Syntax
![Page 8: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/8.jpg)
8
flag .FILL x0001counter .FILL x0002letter .FILL x0041 ; Aletters .FILL x4241 ; BA
LC-3 Syntax
.FILL.FILL
• One declaration per lineOne declaration per line• Always declaring 16-bits, the word Always declaring 16-bits, the word
size of LC-3size of LC-3
![Page 9: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/9.jpg)
9
.BLKW.BLKW
• Tells assembler to set aside some Tells assembler to set aside some number of sequential memory locationsnumber of sequential memory locations
• Useful for arraysUseful for arrays• Can be initializedCan be initialized
LC-3 Syntax
![Page 10: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/10.jpg)
10
Examples of .BLKW:Examples of .BLKW:
;set aside 3 locations;set aside 3 locations.BLKW.BLKW 33
;set aside 1 location and label it.;set aside 1 location and label it.BobBob .BLKW.BLKW 11
;set aside 1 location, label and init to 4.;set aside 1 location, label and init to 4.NumNum .BLKW.BLKW 11 #4#4
LC-3 Syntax
![Page 11: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/11.jpg)
11
.STRINGZ.STRINGZ
• Used to declare a string of charactersUsed to declare a string of characters• Is terminated by x0000Is terminated by x0000
Example:Example:
hellohello .STRINGZ.STRINGZ “Hello World!”“Hello World!”
LC-3 Syntax
![Page 12: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/12.jpg)
12
.END.END
• Tells the assembler where your Tells the assembler where your program endsprogram ends
• Only one per allowed in your Only one per allowed in your programprogram
LC-3 Syntax
![Page 13: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/13.jpg)
13
An immediate is a value specified in an instruction, not by a .FILL declaration
““LC-3”LC-3” ““C”C”LD R1, XLD R1, XLD R2, YLD R2, YADD R3, R2, #0ADD R3, R2, #0 Z = YZ = YADD R3, R1, R2ADD R3, R1, R2 Z = X + YZ = X + Y?????? Z = X – YZ = X – Y?????? Z = X * YZ = X * Y?????? Z = X / YZ = X / YST R3, ZST R3, Z
LC-3 Syntax
![Page 14: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/14.jpg)
14
.ORIG x3000LD R2, ZeroLD R0, M0LD R1, M1
Loop BRz DoneADD R2, R2, R0ADD R1, R1, -1BR Loop
Done ST R2, ResultHALT
Result .FILL x0000Zero .FILL x0000M0 .FILL x0004M1 .FILL x0002
Simple LC-3 programSimple 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?
![Page 15: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/15.jpg)
15
Program Execution
• Assembler translates to executable – machine Assembler translates to executable – machine languagelanguage
• Linker combines multiple LC-3 files – if anyLinker combines multiple LC-3 files – if any• Loader puts executable into memory and Loader puts executable into memory and
makes the CPU jump to first instruction, .ORIG.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 this case, assemble again, too, since data is
in program.in program.
![Page 16: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/16.jpg)
16
if (condition) statement;
else statement;
LC-3 Programming
HLL – if/else statements…HLL – if/else statements…
![Page 17: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/17.jpg)
17
“LC-3” LD R0, countBRnz endifADD R0, R0, #1
endif ; next instruction goes here
LC-3 Programming
“C” if (count < 0) count = count + 1;
![Page 18: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/18.jpg)
18
Loops can be built out of IF’s – WHILE:
“C” while (count > 0)
{a += count;
count--;}
LC-3 Programming
![Page 19: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/19.jpg)
19
“LC-3”
LD R1, aLD R0, count
while BRnz endwhileADD R1, R1, R0ADD R0, R0, #-1BR while
endwhile ST R1, aST R0, count
LC-3 Programming
![Page 20: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/20.jpg)
20
Procedure CallsProcedure CallsSimple 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
![Page 21: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/21.jpg)
21
LC-3 Procedures
JSR Sub ; calls procedure…
; calculate R2 = R0-R1Sub NOT R2, R1
ADD R2, R2, #1ADD R2, R2, R0RET ; returns to line after
; JSR Sub
Example
![Page 22: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/22.jpg)
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
![Page 23: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/23.jpg)
23
“LC-3”LD R0, aLD R1, bJSR Sub
repeat BRpz secondifADD R0, R0, #1
secondif BRnz untilADD R0, R0, #-1
until JSR SubBRnp repeat
LC-3 Procedures
![Page 24: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/24.jpg)
24
“C”for ( I = 3; I <= 8; I++)
{ a = a+I;}
For loops
LC-3 Procedures
![Page 25: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/25.jpg)
25
““LC-3” LC-3” LDLD R0, aR0, aANDAND R1, R1, #0R1, R1, #0 ; init I to zero; init I to zeroADDADD R1, R1, #3R1, R1, #3 ; now make 3; now make 3forfor ADDADD R2, R1, #-8 R2, R1, #-8 BRpBRp endforendforADDADD R0, R0, R1R0, R0, R1 ; a=a+I; a=a+IADDADD R1, R1, #1R1, R1, #1 : I++: I++BRBR forforendforendfor
LC-3 Procedures
![Page 26: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/26.jpg)
26
TRAPTRAP(System Calls)(System Calls)
Use the “TRAP” instruction and a Use the “TRAP” instruction and a “trap vector”.“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.
![Page 27: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/27.jpg)
27
Trap Trap VectorVector
AssembleAssembler Namer Name Usage & ResultUsage & Result
0x200x20 GETCGETC Read a character from console into R0, not Read a character from console into R0, not echoed. echoed.
0x210x21 OUTOUT Write character in R0 to console.Write character in R0 to console.0x220x22
PUTSPUTSWrite string of characters to console. Start Write string of characters to console. Start with character at address contained in R0. with character at address contained in R0. Stops when 0x0000 is encountered.Stops when 0x0000 is encountered.
0x230x23ININ
Print a prompt to console and read in a Print a prompt to console and read in a single character into R0. Character is single character into R0. Character is echoed.echoed.
0x240x24
PUTSPPUTSP
Write a string of characters to console, 2 Write a string of characters to console, 2 characters per address location. Start with characters per address location. Start with characters at address in R0. First [7:0] and characters at address in R0. First [7:0] and then [15:0]. Stops when 0x0000 is then [15:0]. Stops when 0x0000 is encountered.encountered.
0x250x25 HALTHALT Halt execution and print message to Halt execution and print message to console.console.
Trap Service RoutinesTrap Service Routines
![Page 28: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/28.jpg)
28
To print a character; address of the char must be in R0.TRAP x21
orOUT
To read in a character ; will go into R0, no echo.
TRAP x20 or
GETC
Trap Examples
![Page 29: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/29.jpg)
29
To end your program:To end your program:
TRAPTRAP x25x25oror
HALTHALT
SYS Calls Examples
![Page 30: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/30.jpg)
30
Questions?Questions?
![Page 31: LC-3 Assembly Language](https://reader036.fdocuments.net/reader036/viewer/2022081422/56816865550346895ddec180/html5/thumbnails/31.jpg)
31