Upload
mizell
View
42
Download
3
Tags:
Embed Size (px)
DESCRIPTION
LC-3 Assembly Language. Patt and Patel Ch. 7. LC-3 is a load/store RISC architecture. Has 8 general registers Has a flat 16-bit addressing range Has a 16-bit word size Load variables from memory to register. Syntax of LC-3. One instruction, declaration per line - PowerPoint PPT Presentation
Citation preview
1
Patt and Patel Ch. 7Patt and Patel Ch. 7
LC-3 LC-3 Assembly LanguageAssembly Language
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
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.
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
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
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”
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
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
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
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
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
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
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
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?
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.
16
if (condition) statement;
else statement;
LC-3 Programming
HLL – if/else statements…HLL – if/else statements…
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;
18
Loops can be built out of IF’s – WHILE:
“C” while (count > 0)
{a += count;
count--;}
LC-3 Programming
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
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
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
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
“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
24
“C”for ( I = 3; I <= 8; I++)
{ a = a+I;}
For loops
LC-3 Procedures
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
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.
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
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
29
To end your program:To end your program:
TRAPTRAP x25x25oror
HALTHALT
SYS Calls Examples
30
Questions?Questions?
31