tutorial lc-3 and assembly 2 · example: take the digits of the multiplier one at a time from right...

18
Tutorial LC-3 and Assembly 2

Upload: lengoc

Post on 13-Sep-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Tutorial – LC-3 and Assembly 2

Multiplication algorithm using shifts and adds◦ This approach can be faster for „some‟ hardware

http://en.wikipedia.org/wiki/Multiplication_algorithm

On the LC-3 no shift instruction -> use the ADD instruction instead

Example: take the digits of the multiplier one at a time from right to left, multiplying the multiplicand by a single digit of the multiplier and placing the intermediate product in the appropriate positions to the left of the earlier results. (http://users.utcluj.ro/~baruch/book_ssce/SSCE-Shift-Mult.pdf)

Multiplicand 1000 x 8 x Multiplier 1001 9 1000 00000 000000 1000000 Product 1001000 72

Example code segment on next slide: (NOTE: Restore2 subroutine not shown, and

DoRangeCheck is the next code block.)

E.g. 8 * 9 = 72

R0 stores result

R1 contains the multiplicand (e.g. 8)

R2 contains the multiplier (e.g. 9)

R4 contains the bit mask ◦ Determines if multiplier has a 1 at a certain bit

position

.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 x0005

.END

• 1)

• A) What does this program do?

• B) What value will be contained in RESULT after the program runs to completion?

Answers:

A) The program multiplies 4 by 5.

B) The value in RESULT will be 20.

2) When the following LC-3 program is executed, how many times will the instruction at the memory address labelled LOOP execute?

.ORIG x3005

LEA R2, DATA

LDR R4, R2,#0

LOOP ADD R4, R4, #-3

BRzp LOOP

TRAP x25

DATA .FILL x000B

.END

Answer:

The line LOOP is executed 4 times.

Remember, 0xB = #11

.ORIG x3000

ST R0,#7

LEA R0,LABEL

TRAP x22

TRAP x25

LABEL .STRINGZ "COMPSCI 210”

LABEL2 .STRINGZ "HELLO WORLD”

.END

3) The following LC-3 program is assembled and then executed. There are no assemble or run-time errors. Whatis the output (to the console) of this program? Assume all registers are initialisedto 0 before the program executes.

Suggestion: try this code in the LC-3 simulator!

Answer:

The output to the console is “COMP”◦ For “ST R0,#7” the value 7 is added to the

incremented PC, i.e. x3001 + x7 = x3008

◦ R0 has the value zero, therefore “ST R0,#7” sets x3008 to #0, which is the null terminator for a string.

◦ The TRAP x22 service routine is PUTS, which prints a null terminated ASCII string.

4) Assemble the following program:.ORIG x3000

STI R0, LABEL

OUT

HALT

LABEL .STRINGZ "%”

.END

Continued….->

A) The programmer intended the program to output a % to the monitor, and then halt. Unfortunately, the programmer got confused about the semantics of each of the opcodes(that is, exactly what function is carried out by the LC-3 in response to each opcode). Replace exactly ONE opcode in this program with the correct opcode to make the program work as intended.

->

B) The original program from part A) was executed. However, execution exhibited some very strange behaviour. The behaviour was in part due to the programming error and in part due to the fact that the value in R0 when the program started executing was x3000. Explain what the behaviour was and why the program behaved that way.

Answers:

A) Replace the line “STI R0, LABEL” with “LD R0, LABEL”

B) The program outputs nothing to the console when R0 = x3000

OUT uses the lower bits for output.

The program also stores the value x3000 at address x0025, specified by the memory address LABEL, this is because x25 is the ASCII code for %

5) SUBROUTINES!

The following program is supposed to print the number 5 on the screen. It does not work. Why?

.ORIG x3000JSR AOUT BRnzp DONE

A AND R0, R0, #0ADD R0, R0, #5JSR BRET

DONE HALTASCII .FILL x0030B LD R1, ASCII

ADD R0, R0 ,R1RET.END

Answer: This shows an example of nested subroutines done wrong.

With JSR the return address is stored in R7.

R7 is not saved and restored in the A subroutine and the program cannot return to the correct address.

It gets stuck on the line “RET”.

From Patt and Patel: .END

◦ .END tells the assembler where the program ends. Any characters that come after .END will not be used by the assembler. Note:.END does not stop the program during execution. In fact, .END does not even exist at the time of execution. It is simply a delimeter - it marks the end of the source program.

TRAP x25 or HALT◦ Halt execution and print a message on the console

Reading in ASCII digits and interpreting them as numbers:◦ Refer to Section 10.4 (pp. 272) in the course textbook.