CHAPTER 7 9 2 ASSEMBLY LANGUAGE Adapted from

CHAPTER 7 & 9. 2 ASSEMBLY LANGUAGE Adapted from slides provided by Mc. Graw-Hill Companies Inc. , modified by professors at University of Wisconsin-Madison

Why machine language? + The closest language to H/W. Allows us to control exactly what the processor does. - Too tedious to write and debug. 0’s and 1’s make our head spin!

Assembly Language � Human readable Machine language �Instruction pneumonic instead of opcode � Some added functionality �Labels to refer to memory locations � Assembler: A program which converts the human readable form to machine instructions.

Assembly language syntax Each line of an assembly program may be � Instruction �LC-3 instruction �An assembler directive (pseudo-operation) �A comment (Blank lines are ignored) (Whitespaces and case are ignored) (Comments are ignored)

Instruction LABEL OPCODE OPERANDS ; COMMENTS optional mandatory

LC-3 Opcodes � Reserved symbols/names to represent the LC-3 instruction type � Whatever we used to call them in Chapter 5! � Example: ADD, BR, LD, STR, …

Operands � Registers: Rn where n is register number � Numbers: decimal(#50) or hex (x 32) � Labels: location Symbolic name for memory

Multiple Operands � Separate � Same � For multiple operands by comma order as in the binary instruction example: 0001 010 0 00 101 ADD R 1 R 2 R 5 ADD R 1, R 2, R 5

Labels � Placed at the beginning of a line � Symbolic name for the memory address corresponding to that line. � For example: LOOP ADD R 1, #-1 BRp LOOP � Used to replace PCOffset in LC-3 instructions (LD, ST, LDI, STI, BR, JSR)

Labels (restrictions) � Can be any alpha-numeric string �Can include underscore ‘_’ in Penn. Sim � Should start with an alphabet �Unenforced in Penn. Sim � 1 - 20 characters long � Cannot be any of the reserved symbols of LC-3 (like ADD, . ORIG, 19, x. A 3 etc. )

Comments � Anything � Ignored � Can after the semicolon ‘; ’ by the assembler. be used by the programmer to �Document code �Separate pieces of the program

Assembler directives � Pseudo operations � Not an instruction executed by the program, but used by the assembler � Opcode with a dot ‘. ’

Assembler directives Opcode Operand Meaning . ORIG address starting address of program . END end of program . FILL n allocate one word, initialize with value ‘n’ . BLKW n allocate ‘n’ words of storage . STRINGZ n-character string allocate n+1 locations, initialize w/characters and null terminator

Pseudo-Instructions Names for various TRAP codes so that you don’t need to remember them! Code Equivalent Description HALT TRAP x 25 Halt execution and print message to console. IN TRAP x 23 Print prompt on console, read (and echo) one character from keybd. Character stored in R 0[7: 0]. OUT TRAP x 21 Write one character (in R 0[7: 0]) to console. GETC TRAP x 20 Read one character from keyboard. Character stored in R 0[7: 0]. PUTS TRAP x 22 Write null-terminated string to console. Address of string is in R 0.

A full program ; ; Program to multiply a number by the constant 6 ; . ORIG x 3050 LD R 1, SIX LD R 2, NUMBER AND R 3, #0 ; Clear R 3. It will ; contain the product. ; The inner loop ; AGAIN ADD R 3, R 2 ADD R 1, #-1 ; R 1 keeps track of BRp AGAIN ; the iteration. ; HALT ; NUMBER. BLKW 1 SIX. FILL x 0006 ; . END

Pseudo-code to Assembly R 1 <- R 2 + R 3 ADD R 1, R 2, R 3 R 1 <- R 1 AND 10 AND R 1, #10 OR AND R 1, x. A
![Pseudo-code to Assembly R 5 = M[M[x 3020]] (address x 3020 has a label Pseudo-code to Assembly R 5 = M[M[x 3020]] (address x 3020 has a label](http://slidetodoc.com/presentation_image_h2/558659b6ccae38f767f0f651c7725f93/image-17.jpg)
Pseudo-code to Assembly R 5 = M[M[x 3020]] (address x 3020 has a label “NUMBER”) LDI R 5, NUMBER BR on Z or P to x 3030 (address x 3030 has a label “LOOP”) BRzp LOOP
![Pseudo-code to Assembly R 6 = M[R 2+5] LDR R 6, R 2, #5 Pseudo-code to Assembly R 6 = M[R 2+5] LDR R 6, R 2, #5](http://slidetodoc.com/presentation_image_h2/558659b6ccae38f767f0f651c7725f93/image-18.jpg)
Pseudo-code to Assembly R 6 = M[R 2+5] LDR R 6, R 2, #5 BR unconditionally to x 301 E (address x 301 E has a label “NEXT”) BRnzp NEXT OR BR NEXT

Pseudo-code to Assembly Write data x 5000 to memory (add a label “ADDR” to that mem loc) ADDR. FILL x 5000 Store a string “Hi” to memory (add a label “STR” to that mem loc) STRINGZ “Hi”

Writing an Assembly Program Count the number of occurrences of a character in a string. [OR] Set the value of x 4000 to the count of occurrences of a character, stored at x 4001, in a null-terminated string, starting at x 4002

Assembler � Program which converts an assembly language program into machine code. � In programming terms, converts your assembly language file (. asm/. txt) into an executable (. obj) file. � We will use the inbuilt assembler in Penn. Sim

Assembly process � Two pass process

Assembly Process: 1 st Pass � Scan the program file and create a “Symbol Table” � Symbol Table: A table which contains the addresses of all the labels in your assembly language program.

1 st Pass: Steps 1. Find the. ORIG statement, which tells us the address of the first instruction. � Initialize location counter (LC), which keeps track of the current instruction. 2. For each non-empty line in the program: � If line contains a label, add label and LC to symbol table. � 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.

1 st Pass: Practice ; ; Program to multiply a number by the constant 6 ; . ORIG x 3050 LD R 1, SIX LD R 2, NUMBER AND R 3, #0 ; Clear R 3. It will ; contain the product. ; The inner loop AGAIN ADD R 3, R 2 ADD R 1, #-1 ; R 1 keeps track of BRp AGAIN ; the iteration. ; END HALT ; RAND. FILL x 41 FF GREET. STRINGZ “ 252!” NUMBER. BLKW 2 SIX. FILL x 0006. END

1 st Pass: Practice Symbol Address AGAIN x 3053 END x 3056 RAND x 3057 GREET x 3058 NUMBER x 305 D SIX x 305 F

Assembly Process: 2 nd Pass � Generate machine code for each line of assembly statement � Use the symbol table to get the addresses of the labels, so as to calculate the PCOffset

Assembly Process: 2 nd Pass � Errors identified in this pass �Improper number or type of arguments ○ ex: NOT R 1, #7 ADD R 1, R 2 ADD R 3, NUMBER �Immediate argument too large ○ ex: ADD R 1, R 2, #1023 �Address (associated with label) more than “specifiable by offset” from instruction

2 nd Pass: Practice For the previous example, generate machine code for instructions at address: � x 3050: � x 3055:

2 nd Pass: Practice For the previous example, generate machine code for instructions at address: � x 3050: 0010 001 000001110 � x 3055: 0000 001 111111101

Issues � Assembly done. Great! How to execute the code? � How to write code in a team? Many people writing many assembly files? � How to use vendor defined libraries?

Loading � Process of copying an executable (. obj) into memory. � We can load multiple. obj files, each starting at a desired address. � User code: x 3000 - x. FDFF � Careful! Do not overlap the object files

Linking � Process of resolving symbols between independent object files �Suppose we want to use a symbol (label) defined in a different. obj file �some notation, such as. EXTERNAL, is used to tell assembler that a symbol is defined in another module �linker will search symbol tables of other modules to resolve symbols and complete code generation before loading

Subroutines (Section 9. 2) � Very similar to “functions” in High Level Languages Arguments foo() {. . . c = a + b d = bar(c, a) e = d + 3. . . } Caller Function Return Value bar(x, y) { z = x * y return z } Callee/Called Function

Subroutines �A program fragment the performs a well defined task � Invoked � Returns (or called) by a user program control when the called program has finished

Arguments & Return Values � Arguments �The value(s) passed into a subroutine � Return Values �The value(s) passed out of a subroutine

Why subroutines? � Reuse useful and debugged code, rather than redoing work � Divide task among multiple programmers � Use vendor supplied library of routines

How to call a subroutine? � Use assembly instructions: JSR or JSRR � Jumps to a location, like BR, but unconditionally � Before jumping, copies (saves) PC into R 7 � Can use this “saved PC” to return to the caller program

JSR & JSRR � Even though two assembly instructions, same OPCODE in binary � Differ in bit 11 of the instruction Instruction JSRR Bit 11 1 0 Target Address PC’ + PCOffset 11 Base Register

JSR (PC-Relative) 15 14 13 12 11 0 0 1 10 9 8 7 6 5 4 PCoffset 11 3 2 1 0

JSRR (Register) 15 14 13 12 11 10 9 8 7 6 0 1 0 0 0 Base Reg 5 4 3 2 1 0 0 0 0

JSR & JSRR What is the advantage of JSRR over JSR?

How to return to the caller? � Using � Alias JMP the RET assembly instruction (pseudo-instruction) for JMP R 7 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 0 0 0 Base Reg 0 0 0 Fix Base Register as R 7 RET 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 0 0 0 1 1 1 0 0 0

Arguments and Return Values � Normally registers are used to �Pass arguments from caller to callee �Return value from callee to caller � Registers being used to pass arguments and return values should be explicitly stated when writing a subroutine

Example ; value is R 0 is negated and ; Argument: R 0 ; Return: R 0 2 s. Comp NOT R 0, R 0 ADD R 0, #1 RET returned ; flip bits ; add one ; return to caller Note: Call must be from within 1024 memory locations! . . . ; need to compute ADD JSR ADD. . . R 4 = R 1 - R 3 R 0, R 3, #0 ; copy R 3 to R 0 2 s. Comp ; negate R 4, R 1, R 0 ; add to R 1

Other Issues � The caller and the callee use the same 8 General Purpose Registers (R 0 -R 7) � What if the callee modifies registers which are being used by the caller? � Let us revisit our example and modify the subroutine:

Example R 1 modified! ; value is R 0 is negated and ; Argument: R 0 ; Return: R 0 2 s. Comp NOT R 0, R 0 R 1, ADD R 0, R 1, #1 RET returned ; flip bits ; add one ; return to caller Note: Call must be from within 1024 memory locations! . . . ; need to compute ADD JSR ADD. . . R 1’s value has changed! R 4 = R 1 - R 3 R 0, R 3, #0 ; copy R 3 to R 0 2 s. Comp ; negate R 4, R 1, R 0 ; add to R 1
![Two solutions! � Caller-save �[problem solved by the caller] � Callee-save �[problem solved by Two solutions! � Caller-save �[problem solved by the caller] � Callee-save �[problem solved by](http://slidetodoc.com/presentation_image_h2/558659b6ccae38f767f0f651c7725f93/image-48.jpg)
Two solutions! � Caller-save �[problem solved by the caller] � Callee-save �[problem solved by the callee]

Caller-save � Caller knows which registers are modified by the callee � Caller saves those registers before the call and restores them after the call � Generally, known modified registers are not �Solution: Save all registers

Example ; value is R 0 is negated and ; Argument: R 0 ; Return: R 0 2 s. Comp NOT R 1, R 0 ADD R 0, R 1, #1 RET returned ; flip bits ; add one ; return to caller . . . ; need to compute R 4 = R 1 - R 3 ADD R 0, R 3, #0 JSR ST 2 s. Comp R 1, SAVE_R 1 ADD JSR R 4, R 1, R 0 2 s. Comp. . . LD R 1, SAVE_R 1 ADD R 4, R 1, R 0. . . SAVE_R 1. BLKW 1 ; ; ; copy R 3 to R 0 negate Save R 1 add to R 1 negate Restore R 1 add to R 1

Callee-save � Callee saves the registers which it will modify at the beginning of the subroutine � Restore the values before RET

Example ; value is R 0 is negated and returned ; Argument: R 0 ; Return: R 0 2 s. Comp NOT ST R 1, SAVE_R 1; R 0 ; Save flip bits R 1 ADD R 0, one NOT R 1, R 0 #1 ; ; add flip bits RET to caller ADD R 0, R 1, #1 ; ; return add one LD R 1, SAVE_R 1; Restore R 1 RET ; return to caller SAVE_R 1. BLKW 1. . . ; need to compute ADD JSR ADD. . . R 4 = R 1 - R 3 R 0, R 3, #0 ; copy R 3 to R 0 2 s. Comp ; negate R 4, R 1, R 0 ; add to R 1

Notes � If the caller is a subroutine, it should save R 7. � Even if using callee-save, value of R 7 has to be saved and restored by the caller.

Summary

Assembly Instruction LABEL OPCODE OPERANDS ; COMMENTS optional mandatory

Various parts of the instruction Label Symbolic name for the memory address of the instruction Opcode • LC-3 instruction opcode • Assembler directive Operands • Register: R 0 -R 7 • Numbers: decimal (#10) or hexadecimal (x. A) • Labels Comments • Ignored by the assembler • Used by programmers for documentation

Assembler Directives . ORIG x 3050. FILL x 35. BLKW #3. STRINGZ “Hi!”. FILL #25. END Address Value x 3050 x 0035 x 3051 x 0000 x 3052 x 0000 x 3053 x 0000 x 3054 x 0048 x 3055 x 0069 x 3056 x 0021 x 3057 x 0000 x 3058 x 0019

Assembly Process � 2 Pass Process � 1 st Pass �Generate the symbol table �[Symbol Table: Mapping of label to address] � 2 nd Pass �Generate machine code �Use symbol table to change labels to PCOffset

Loading and Linking � Loading �Process of loading an executable to memory �Multiple executable can be loaded into memory at the same time � Linking �Process of resolving symbols defined in a different assembly file

Subroutines � Calling �JSR or JSRR �Save PC to R 7 � Returning �RET or JMP R 7 � Arguments and Return values �Using registers (need to be stated explicitly)

Saving and Restoring � Caller-save and Callee save � Caller save advantage + knows which registers it needs after the call - doesn’t know which registers are modified by the callee. � Callee save + knows which registers it is going to modify - doesn’t know which registers caller needs.

Problems

Problem 1. ORIG x 3000 XAF ADD R 0, x 1 F LOOP BRnz NEXT BRp ZERO LDR R 1, Zero NOT R 3, #5 OR R 1, R 2, R 1 HALT Zero. FILL 0 LOOP. BLKW 2 CHAR. ASCII ‘a’ STRINGZ “Time”. END Find the assembly errors in the program.

Solution. ORIG x 3000 XAF ADD R 0, x 1 F LOOP BRnz NEXT BRp ZERO LDR R 1, Zero NOT R 3, #5 OR R 1, R 2, R 1 HALT Zero. FILL 0 LOOP. BLKW 2 CHAR. ASCII ‘a’ STRINGZ “Time”. END

Problem 2. ORIG x 3800 LEA R 3, INPUT LD R 1, SIZE ADD R 3, R 1 LOOP LDR R 0, R 3, 0 TRAP x 21 ADD R 3, -1 ADD R 1, -1 BRp LOOP HALT INPUT. STRINGZ "Rtb. Y" STRING. BLKW #3 SIZE. FILL #3 TWO. FILL #2. END (a) Generate the symbol table for the above code. (b) What are the values in memory (in hex) at the following memory addresses: x 3800 x 3807 x 380 B x 3811


Solution Label Address ---------INPUT x 3809 LOOP x 3803 SIZE x 3811 STRING x 380 E TWO x 3812 Address Value ---------x 3800 x. E 608 x 3807 x 03 FB x 380 B x 0062 x 3811 x 0003

Problem 3. ORIG X 3100 LEA R 0, TRS LD R 1, SUM LOOP LDR R 2, R 0, #0 BRz END ADD R 2, R 1 STR R 2, R 0, #0 ADD R 0, #2 BR LOOP END HALT SUM. FILL x 20 TRS. STRINGZ "BYEBYE". END What does this program do?


Solution Changes the string stored at TRS from “BYEBYE” to “b. Ye. By. E”

Problem 4 SUB 1 SUB 2 . ORIG x 3000 ST R 0, SAVER 0 ST R 1, SAVER 1 JSR SUB 1 LD R 0, SAVER 0 LD R 1, SAVER 1 HALT ADD R 0, R 1 JSR SUB 2 AND R 2, R 1, R 0 RET ST R 3, SAVER 3 AND R 3, R 0 NOT R 0, R 3 LD R 3, SAVER 3 RET. END (a) Is there is a problem with the above assembly language program? How will you fix it? (b) Is SUB 1 caller-save or callee-save? Is SUB 2 caller-save or callee-save?

Solution � R 7 has to be saved before call from SUB 1 to SUB 2 � SUB 1: Caller-save � SUB 2: Callee-save

Problem 5 � What single instruction is equivalent to the following two LC-3 instructions? LEA R 7, #1 JMP R 2, #0 � RET instruction is executed at address x 1234. What is the range of possible values of PC after this instruction executes? � Which assembler directive(s) can be used only once in an assembly language program?

Solution � JSRR R 2 � 0 x 0000 – 0 x. FFFF �RET is “JMP R 7” �. ORIG and. END
- Slides: 74