Computer Sciences Dept.

CS/ECE 252 Introduction to Computer Engineering

Fall 2014 Section 2
Instructor Guri Sohi
TAs Lisa Ossian, Sujith Surendran, Minsub Shin

URL: http://www.cs.wisc.edu/~sohi/cs252/Fall2014/

Homework 7 [Due at lecture on Fri, Nov 21]

Primary contacts for this homework: Sujith Surendran [sujiths at cs dot wisc dot edu], Lisa Ossian [ossianli at cs dot wisc dot edu]

You must do this homework alone. Please handin the copy of the homework listing your section number, full name (as appear in Learn@UW) and UW ID. You must staple all pages of your homework together to receive full credit.

Submission guidelines:

  • Answers to Problem 1, Problem 2, and Problem 3 should be handed in hard copy.
  • Problem 4 and Problem 5 must be submitted electronically to the Learn@UW dropbox. Submit only one archive file, named exactly like: <lastname>[.zip] to the dropbox folder homework7. Your .zip file should contain the .asm files for these problems (which contains the assembly code) and the files should be named hw7_p#.asm, i.e, the file for Problem 4 should be named hw7_p4.asm. Since your code is tested automatically, it is important to stick to this naming convention, otherwise you will lose credit, even if your code is working correctly.
  • The programs which you write should always start at address 0x3000 and end with a HALT instruction (HALT).
  • You can submit your code for Problem 4 and Problem 5 as many times as you want, until 1:20 PM on Nov 21, 2014. After that time we will consider your latest submission for grading.
It is important that you follow the above submission guidelines since your code submission will be graded automatically. Any submission that deviates from the above guidelines will be penalized.

Problem 1 (3 points)

Identify 3 assembly errors in the following program:

        .ORIG x3001
        AND R5, R5, ZERO    ;The AND instruction can have either a register or an immediate value for the second operand.
        LD R5, STRING
NEXT    ADD R5, R5, #1
        BRz NEXT
        LDR R4, R5, #0
        SUB R4, R4, #1      ;The LC-3 has no SUB instruction.
        ST R4, STRING
NEXT    HALT                ;There is already a label called NEXT.

ZERO    .FILL #0
STRING  .STRINGZ "Hurray !!!"
        .END

Problem 2 (4 points)

Consider the following assembly program:

        .ORIG X3000
	AND R4, R4, #0
        LD R5, INPUT
LOOP    BRz END
        BRp SKIP
        ADD R4, R4, #1
SKIP    ADD R5, R5, R5
        BR LOOP
END	ST  R4, RESULT
        HALT

INPUT   .FILL x1234
RESULT  .BLKW 1
        .END

  1. (2 points) Run the program in PennSim and give a brief explanation of what the program does. (i.e., Specify how the value at RESULT relates to value at the INPUT after the execution of the program.)
  2. RESULT contains the number of 1s in the value at INPUT

  3. (1 point) How many times does the instruction at label LOOP execute?
  4. 15 times

  5. (1 point) What value will be contained in R5 after the execution of the program?
  6. 0

Problem 3 (5 points)

Consider the following assembly code:

        .ORIG x4000
        LEA R3, INPUT
        LD R1, SIZE
        ADD R3, R3, R1
LOOP    LDR R0, R3, 0
        ADD R3, R3, -1
        ADD R1, R1, -1
        BRp LOOP
        HALT
INPUT   .STRINGZ "I Like LC-3"
STRING  .BLKW 4
INPUT2  .FILL x1
SIZE    .FILL x12
        .END
  1. (2 points) In the first pass, the assembler creates the symbol table. Fill in the symbol table created by the assembler for this program. (You may extend this table to any number of rows.)

           Symbol          Address     
    LOOPx4003
    INPUTx4008
    STRINGx4014
    INPUT2x4018
    SIZEx4019

  2. (1 point) Does the symbol table contain entries for .ORIG and .END? Why or Why not?
  3. No, because they are not labels. .ORIG and .END are assembly directives.

  4. (2 points) In the second pass, the assembler creates a binary (.obj) version of the program, using the entries from the symbol table. Write the binary code generated for the first two instructions (LEA and LD).
  5. 1110 011 000000111
    0010 001 000010111

Problem 4 (10 points)

Assume that you are a TA for this CS/ECE 252 course in Fall 2015, and assume that 20 students have enrolled in this course. After grading all their exams, you finally decided to use an LC-3 program for calculating the performance of the class. For doing so, assume you have stored the total score of each of the students in consecutive memory locations starting at 0x4000.

Write an assembly language program in LC-3, which starts at memory location 0x3000, to do the following:

  1. (2 points) Calculate maximum score obtained by a student and store this value in the memory location corresponding to label "MAX_SCORES" (see template hw7_p4.asm).
  2. (2 points) Calculate minimum score obtained by a student and store this value in the memory location corresponding to label "MIN_SCORES".
  3. (4 points) Calculate the average score of the class and store this value in memory location corresponding to label "AVG_SCORES". If this turns out to be a fraction, round it off to the next higher integer. For example, if the average turns out to be 30.02, the average should be rounded off to 31.
  4. (2 points) Calculate the number of students who have scored below the class average (which has been rounded off) and store this value in memory location corresponding to label "BELOW_AVG".

Note:

  • You MUST use this template for writing your code: hw7_p4.asm. You can assume that addition of scores of all the students will not generate overflow.
  • You may use the script file (hw7p4_script.txt) for testing your code. The script has some default values for scores for each student. However, please note that we will be finally testing your code with other values for scores (not with the default ones).
    • Before you use the script, make sure that your code is getting compiled successfully.
    • Then you can open Pennsim (if it is closed) and type "script hw7p4_script.txt" in the command line of Pennsim. This script will reset the system, load your code and OS and then test your code with some default values of scores. If any of the tests fail, you will get a FALSE output on the screen. If all tests are successful, you will see the following 4 true statements (for the default values):
      • TRUE (check MAX_SCORES 100)
      • TRUE (check MIN_SCORES 70)
      • TRUE (check AVG_SCORES 84)
      • TRUE (check BELOW_AVG 10)
Solution: hw7_p4_sol.asm

Problem 5 (8 points)

There exists a file of 2's complement numbers (each between 0 and 9). The address of the first number in the file is present in a memory location with the label "INPUT_ADDRESS" (See hw7_p5.asm). Each number is in one word of memory. Write an LC-3 assembly program which multiplies each number in this file by the value present at memory location "MULTIPLIER". Store the final result into another file in memory, whose starting address is contained in the memory location corresponding to label "PRODUCT_ADDRESS", again with each number in the file in a word of memory.

  • Use this template for writing your code: hw7_p5.asm.
  • The INPUT file is terminated by a NULL character. The output file should also be terminated by a NULL character.
Solution: hw7_p5_sol.asm






 
Computer Sciences | UW Home