Understanding Assembly Language Programming for Computing Layers

Slide Note
Embed
Share

Assembly language is a low-level programming language that enables direct interaction with a computer's hardware components. This content explores the fundamentals of assembly language, the relationship between human-readable machine language and binary code, an assembly language program for multiplying a number by six, LC-3 assembly language syntax, opcodes and operands concepts, and the importance of labels and comments in writing efficient assembly language programs.


Uploaded on Sep 21, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Chapter 7 Assembly Language

  2. Computing Layers Problems Algorithms Language Instruction Set Architecture Microarchitecture Circuits Devices

  3. Human-Readable Machine Language Computers like ones and zeros 0001110010000110 Humans like symbols ADD R6,R2,R6 ; increment index reg. Assembler is a program that turns symbols into machine instructions. ISA-specific: close correspondence between symbols and instruction set Mnemonics for opcodes Labels for memory locations Additional operations for allocating storage and initializing data

  4. An Assembly Language Program ; ; Program to multiply a number by six ; .ORIG LD LD AND ; ; The inner loop ; AGAIN ADD ADD BRp ; HALT ; NUMBER .BLKW SIX .FILL ; .END x3050 R1, SIX ; R1 has constant R2, NUMBER ; R2 has variable R3, R3, #0 ; R3 has product R3, R3, R2 ; R3 += R2 R1, R1, #-1 ; R1 is loop counter AGAIN ; conditional branch 1 ; variable x0006 ; constant

  5. LC-3 Assembly Language Syntax Each line of a program is one of the following: Instruction Assembler directive (or pseudo-op) Comment Whitespace (between symbols) and case are ignored. Comments (beginning with ; ) are also ignored. x hex, # decimal, default is decimal An instruction has the following format: LABEL OPCODE OPERANDS ; COMMENTS optional mandatory

  6. Opcodes and Operands Opcodes reserved symbols that correspond to LC-3 instructions listed in Appendix A ex: ADD, AND, LD, LDR, Operands registers -- specified by Rn, where n is the register number numbers -- indicated by # (decimal) or x (hex), default is decimal label -- symbolic name of memory location separated by comma number, order, and type correspond to instruction format ex: ADD R1,R1,R3 ADD R1,R1,#3 LD R6,NUMBER BRz LOOP

  7. Labels and Comments Label Placed at the beginning of the line Assigns a symbolic name to the address corresponding to line ex: LOOP ADD R1,R1,#-1 BRp LOOP Comment Anything after a semicolon is a comment Ignored by assembler Used by humans to document/understand programs Tips for useful comments: Avoid restating the obvious, as decrement R1 Provide additional insight, as in accumulate product in R6 Use comments to separate pieces of program State the purpose of each register

  8. Assembler Directives Pseudo-operations do not refer to operations executed by program used by assembler look like instruction, but opcode starts with dot Opcode Operand address Meaning starting address of program end of program allocate n words of storage .ORIG .END .BLKW n n allocate one word, initialize with value n allocate n+1 locations, initialize w/characters and null terminator .FILL n-character string .STRINGZ

  9. Trap Codes LC-3 assembler provides pseudo-instructions for each trap code, so you don t have to remember them. Unlike pseudo-operations pseudo-instructions do generate operations that will be executed by the program Code HALT Equivalent TRAP x25 Description Halt execution and print message to console. Print prompt on console, read (and echo) one character from keybd. Character stored in R0[7:0]. Write one character (in R0[7:0]) to console. IN TRAP x23 OUT TRAP x21 Read one character from keyboard. Character stored in R0[7:0]. Write null-terminated string to console. Address of string is in R0. GETC TRAP x20 PUTS TRAP x22

  10. Style Guidelines Use the following style guidelines to improve the readability and understandability of your programs: 1. Provide a program header, with author s name, date, etc., and purpose of program. 2. Start labels, opcode, operands, and comments in same column for each line. (Unless entire line is a comment.) 3. Use comments to explain what each register does. 4. Give explanatory comment for most instructions. 5. Use meaningful symbolic names. Mixed upper and lower case for readability. ASCIItoBinary, InputRoutine, SaveR1 6. Provide comments between program sections. 7. Each line must fit on the page -- no wraparound or truncations. Long statements split in aesthetically pleasing manner.

  11. Example Count the occurrences of a character in an array Program begins at location x3000 Read character from keyboard Load each character from array Starting address of array is stored in the memory location immediately after the program If character equals input character, increment counter The end of the array is indicated by a special ASCII value: EOT (x04) At the end, print the number of characters and halt (assume there will be less than 10 occurrences of the character) A special character used to indicate the end of a sequence is often called a sentinel. Useful when you don t know ahead of time how many times to execute a loop.

  12. Flow Chart Count = 0 (R2 = 0) Convert count to ASCII character (R0 = x30, R0 = R2 + R0) YES Done? (R1 ?= EOT) Ptr = 1st file character (R3 = M[x3012]) NO Print count (TRAP x21) Match? (R1 ?= R0) YES NO Input char from keybd (TRAP x23) HALT (TRAP x25) Incr Count (R2 = R2 + 1) Load char from file (R1 = M[R3]) Load next char from file (R3 = R3 + 1, R1 = M[R3])

  13. LC3 Assembly .ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23 LDR R1 ,R3, #0 ; ; Test character for end of file ; TEST ADD R4, R1, #-4 BRz OUT ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 NOT R1, R1 BRnp GETC ADD R2, R2, #1 ; Get next character from the file ; GETC ADD R3, R3, #1 ; Increment the pointer LDR R1, R3, #0 ; R1 gets the next character to test BRnzp TEST ; ; Output the count. ; OUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary to ASCII TRAP x21 ; ASCII code in R0 is displayed TRAP x25 ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x3014 .END ; R2 is counter, initialize to 0 ; R3 is pointer to characters ; R0 gets character input ; R1 gets the next character D A B ; Test for EOT ; If done, prepare the output E C ; If match, R1 = xFFFF ; If match, R1 = x0000 ; no match, do not increment 1) What instruction would need to be changed if the null terminator (0) is used to signal the end of the file 2) What instruction dereferences a pointer to load a character into a register?

  14. LC3 Assembly .ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23 LDR R1 ,R3, #0 ; ; Test character for end of file ; TEST ADD R4, R1, #-4 BRz OUT ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 NOT R1, R1 BRnp GETC ADD R2, R2, #1 ; Get next character from the file ; GETC ADD R3, R3, #1 ; Increment the pointer LDR R1, R3, #0 ; R1 gets the next character to test BRnzp TEST ; ; Output the count. ; OUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary to ASCII TRAP x21 ; ASCII code in R0 is displayed TRAP x25 ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x3014 .END ; R2 is counter, initialize to 0 ; R3 is pointer to characters ; R0 gets character input ; R1 gets the next character D A B ; Test for EOT ; If done, prepare the output E C ; If match, R1 = xFFFF ; If match, R1 = x0000 ; no match, do not increment 4) What check/test could be accomplished with a NOT then an ADD of 1 then an ADD 3) What instruction loads the PC with the value found at an address in the first x100 locations of memory

  15. Assembly Process Convert assembly language file (.asm) into an executable file (.obj) for the LC-3 simulator. First Pass: Scan program file Find all labels and calculate the corresponding addresses. Use these to build the symbol table Second Pass: Convert instructions to machine language, using information from symbol table

  16. First Pass: Constructing the Symbol Table 1. Find the .ORIG statement, which tells us the address of the first instruction. Initialize location counter (LC), which keeps track of the memory address of the current instruction. 2. For each non-empty line in the program: a) If line contains a label, add label and LC to symbol table. b) Increment LC. NOTE: If statement is .BLKW or .STRINGZ, increment LC by the number of words allocated. 3. Stop when .END statement is reached. NOTE: A line that contains only a comment is considered an empty line.

  17. .ORIG x3000 Symbol Table Construct the symbol table for the program TEST ; ;Test character for match, if so increment count. NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; No match, no increment ADD R2, R2, #1 ; ; Get next character from file. GETCHAR ADD R3, R3, #1 ; Point to next cha. LDR R1, R3, #0 ; R1 gets next char BRnzp TEST ; ; Output the count. OUTPUT LD R0, ASCII ; Load ASCII template ADD R0, R0, R2 ; Covert binary to ASCII OUT ; ASCII code is displayed HALT ; Halt machine ; ; Storage for pointer and ASCII template ASCII .FILL x0030 PTR .FILL x4000 .END AND R2, R2, #0 ; init counter LD R3, PTR ; R3 pointer to chars GETC ; R0 gets char input LDR R1, R3, #0 ; R1 gets first char ADD R4, R1, #-4 ; Test for EOT BRz OUTPUT ; done? Symbol TEST GETCHAR OUTPUT ASCII PTR Address x3004 x3013

  18. Second Pass: Generating Machine Language For each executable assembly language statement, generate the corresponding machine language instruction. If operand is a label, look up the address from the symbol table. Potential problems: Improper number or type of arguments ex: NOT R1,#7 ADD R1,R2 ADD R3,R3,NUMBER Immediate argument too large ex: ADD R1,R2,#1023 Address (associated with label) more than 256 from instruction can t use PC-relative addressing mode

  19. Practice Using the symbol table constructed earlier, translate these statements into LC-3 machine language. .ORIG x3000 AND R2, R2, #0 ; init counter LD R3, PTR ; R3 pointer to chars GETC ; R0 gets char input Symbol TEST PTR Address x3004 x3013 Statement Machine Language 0010 011 0 0001 0001 LD R3,PTR Symbol ptr: x3013, LD is at x3001 Offset needed: x13 - x02 (PC incremented)

  20. LC-3 Assembler Using assemble (Unix) or LC3Edit (Windows), generates several different output files. This one gets loaded into the simulator. Show binary version of file: xxd b <file> Show hex version of file: xxd <file>

  21. Object File Format LC-3 object file contains Starting address (location where program must be loaded), followed by Machine instructions Example Beginning of count character object file looks like this: .ORIG x3000 AND R2, R2, #0 ; init counter LD R3, PTR ; R3 pointer to chars GETC ; R0 gets char input .ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23

  22. Multiple Object Files An object file is not necessarily a complete program. system-provided library routines code blocks written by multiple developers LC-3 simulator can load multiple object files into memory, then start executing at a desired address. System routines, such as keyboard input, are loaded automatically Loaded into system memory, below x3000 User code should be loaded between x3000 and xFDFF IO registers above xFDFF Each object file includes a starting address Be careful not to load overlapping object files

  23. Linking and Loading Loading is the process of copying an executable image into memory. More sophisticated loaders are able to relocate images to fit into available memory Must readjust branch targets, load/store addresses Linking is the process of resolving symbols between independent object files. Suppose we declare a label/variable in one module, and want to use it in another Some notation, such as .EXTERNAL, is used to tell assembler that the symbol is defined in another module Linker will search symbol tables of other modules to resolve symbols and complete code generation before loading For fun try creating an LC3 program that uses multiple .obj files More info: https://www.linuxjournal.com/article/6463 Video about how this works with C/C++ programs in Linux: https://youtu.be/dOfucXtyEsU

  24. LC-3 tools Local Modifications The following LC-3 assembly instructions will only work with the local tools in the CS department (they will not work with the tools at the text book web site). Pseudo-instructions: macros that are replaced by one or more actual machine instructions during assembly. .ZERO DR (AND DR,DR,#0) .COPY DR,SR1 (ADD DR,SR1,#0) GETS (Trap #26) NEWLN (Trap #27) Recall, pseudo-operations do not refer to operations executed by program, only used by assembler, and by convention start with a .

  25. LC-3 tools Local Modifications Instruction set extension: (remember there was one unused opcode) PUSH POP The authors had chosen to not implement these in accordance with the minimalist RISC approach (see page 254). NOP The authors had implemented the all 0 instruction (BRnzp with offset 0) so that it is a NOP. In the modified tools the instruction is illegal. A NOP is sometimes used for inserting delays.

  26. What are the values in R0,R1,R2 after the code below executes? Assume the Main label is at address x3000. A. x4321, x3003, x7324 B. x4321, x3004, x7324 C. x4321, x3003, x4321 D. x4321, x3004, x4321 E. None of the above Main LD R0,Data LEA R1,Data LDR R2,R1,0 HALT Data .FILL 0x4321

  27. What is the PC offset field in the ST instruction shown below? Data0 .FILL x1234 Data1 .FILL x2345 Data2 .BLKW 1 Main LD R1,Data0 LD R2, Data1 ADD R3,R2,R1 ST R3,Data2 A. 0b111111011 B. 0b111111100 C. 0b111111101 D. 0b100000100 E. 0b100000101

  28. Match the LD R0, Var assembly instruction to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LD R0, Var C Code: int Var = 4; int *Ptr = &Var; int R0; // what goes here? A. R0 = Var; B. R0 = &Var; C. R0 = *Ptr; D. None of the above E. All of the above

  29. Match the LEA R0, Var assembly instruction to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LEA R0, Var C Code: int Var = 4; int *Ptr = &Var; int R0; // what goes here? A. R0 = Var; B. R0 = &Var; C. R0 = *Ptr; D. None of the above E. All of the above

  30. Match the LDI R0, Ptr assembly instruction to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LDI R0, Ptr C Code: int Var = 4; int *Ptr = &Var; int R0; // what goes here? A. R0 = Var; B. R0 = &Var; C. R0 = *Ptr; D. None of the above E. All of the above

  31. Match the LD R1, PTR and LDR R0, R1, #0 assembly instructions to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LD R1, PTR LDR R0, R1, #0 C Code: int Var = 4; int *Ptr = &Var; int R0, R1; // what goes here? A. R0 = var; B. R0 = &Var; C. R0 = *Ptr; D. R1 = Ptr; R0 = *R1; E. None of the above

Related


More Related Content