hcmut2010 midterm

Upload: ace-nhat

Post on 09-Jan-2016

213 views

Category:

Documents


0 download

DESCRIPTION

Electrical & Electronic Engineering, Introduction to Computer Engineering

TRANSCRIPT

  • ECE 190 Midterm Exam HoChiMinh City University of Technology

    Sunday 18 July 2010

    Be sure your exam booklet has 6 pages (3 sheets). You have THREE HOURS to complete this exam. Write your name at the top of each page. This is a closed book exam. You may not use a calculator. You are allowed two handwritten A4 sheets of notes (both sides). Absolutely no interaction between students is allowed. Show all of your work. Be sure to clearly indicate any assumptions that you make. Dont panic, and good luck!

    (An LC-3 ISA encoding + RTL chart is included in the test.)

    Problem 1 25 points _______________________________ Problem 2 30 points _______________________________ Problem 3 20 points _______________________________ Problem 4 25 points _______________________________ Total 100 points _______________________________

    Name: Student ID # :

  • Page 2 Name: ____________________________________________

    Problem 1 (25 points): Short Answer Part A (5 points): After a user presses the letter k, the KBDR contains the value for k (x6B), and the KBSR contains the value x8000. Assuming that R2=xFE02 (the KBDRs address) and that the LC-3 then executes LDR R1,R2,#0, fill in the table below with correct values after the instruction finishes execution.

    Register Value R1 R2

    MAR MDR KBSR

    Part B (5 points): What might happen if a program reads from the Display Data Register without checking the Display Status Registers ready bit? Part C (5 points): Explain why R7 must be caller-saved for any LC-3 subroutine. (Recall that caller-saved implies that the subroutine can change the value.) Consider the following code snippet

    AND R1, R1, #0 LOOP ADD R0, R0, #0

    BRn DONE ADD R0, R0, R0

    ADD R1, R1, #1 BRnzp LOOP HALT

    Part D (5 points): For what value or values of register R0 does the code not reach the HALT instruction? Part E (5 points): What does the code calculate in R1?

  • Page 3 Name: ____________________________________________

    Problem 2 (30 points): Interpreting Binary Code Part A (10 points): The contents of several memory locations are shown below. For each location other than the TRAP, write a register transfer language (RTL) statement corresponding to the instruction represented by the contents of that location. For example, the word x907F is R0 NOT R1 in RTL. I will not grade the RTL syntax, but what you write must convey the action taken by the LC-3 when processing each instruction. Part B (10 points): The table below shows the values in certain registers before execution of the code in Part A. Fill in the values for all registers after the six instructions execute. Do NOT leave blank entries; you may write unknown if the contents of a register cannot be known. Part C (10 points): In 25 words or less, explain what the code in Part A does.

    Address Content RTL x3015 x1DBF

    x3016 x7180

    x3017 xF020 TRAP x20, or GETC

    x3018 x6F80

    x3019 x7180

    x301A x11E0

    Register Before After PC x3015

    MAR unknown

    MDR unknown

    R0 x5678

    R1 x2345

    R6 xC234

    R7 xABCD

  • Page 4 Name: ____________________________________________

    Problem 3 (20 points): Interpreting Assembly Code This question focuses on the following subroutine written in LC-3 assembly language. MY_FUNCTION ST R7,SAVE_R7 ST R1,SAVE_R1 READ_LOOP GETC OUT LD R1,NEG_0 ADD R0,R0,R1 BRzp MAYBE_OK BAD_KEY AND R0,R0,#0 ADD R0,R0,#-1 BRnzp DONE MAYBE_OK ADD R1,R0,#-10 BRn DONE LD R1,DIFF0A ADD R0,R0,R1 BRn BAD_KEY ADD R1,R0,#-6 BRzp BAD_KEY ADD R0,R0,#10 DONE LD R1,SAVE_R1 LD R7,SAVE_R7 RET SAVE_R1 .BLKW 1 ; storage for saved register values SAVE_R7 .BLKW 1 NEG_0 .FILL xFFD0 ; the additive inverse of ASCII '0' DIFF0A .FILL xFFEF ; ASCII '0' - ASCII 'A' (x-11) Part A (5 points): Explain why the value in R7 is saved to memory and later copied back into R7. Part B (5 points): The subroutine returns a value in R0. What value is returned in R0 if the user presses the letter A during the GETC trap? Part C (5 points): How many different values can be returned in R0? (For example, if R0 were always either 3 or 10 when the subroutine returned, the answer would be 2.) Part D (5 points): Briefly explain what the subroutine does.

  • Page 5 Name: ____________________________________________

    Problem 4 (25 points): C Programming Consider the following C program. #include int y = 5; int foo (int a, int b) { int c = a / b; c = c * 2; y++; while (0 < b) { printf ("b = %d\n", --b); } if (10 > a--) { return a; } else { return c; } } int main () { int x = foo (10, 3); y++; return 0; } Part A (10 points): Write down the output of this program. Part B (10 points): What is the value of x right before main returns? Part C (5 points): What is the value of y right before main returns?

  • LD

    LDI

    LDR

    LEA

    NOT

    ST

    STI

    STR

    ADD

    ADD

    AND

    AND

    BR

    JMP

    JSR

    TRAP

    0001 DR SR1 0 00 SR2

    0001 SR1 1

    000

    imm5

    0101 SR1 0 00 SR2

    0101 SR1 1 imm5

    0000 pzn PCoffset9

    1100 BaseR 000000

    0100 1

    DR

    DR

    DR

    PCoffset11

    1111

    DR SR1 + SR2, Setcc

    ADD DR, SR1, SR2

    DR SR1 + SEXT(imm5), Setcc

    ADD DR, SR1, imm5

    DR SR1 AND SR2, Setcc

    DR SR1 AND SEXT(imm5), Setcc

    AND DR, SR1, SR2

    AND DR, SR1, imm5

    BR{nzp} PCoffset9

    ((n AND N) OR (z AND Z) OR (p AND P)):PC PC + SEXT(PCoffset9)

    JMP BaseR

    PC BaseR

    R7 PC, PC PC + SEXT(PCoffset11)

    JSR PCoffset11

    R7 PC, PC M[ZEXT(trapvect8)]

    TRAP trapvect8

    0010 PCoffset9DR

    1010 PCoffset9DR

    0110 offset6

    1110 PCoffset9DR

    DR BaseR

    1001 111111DR SR

    0011 PCoffset9SR

    1011 PCoffset9SR

    0111 offset6SR BaseR

    LD DR, PCoffset9

    LDI DR, PCoffset9

    LDR DR, BaseR, offset6

    LEA DR, PCoffset9

    NOT DR, SR

    ST SR, PCoffset9

    DR M[PC + SEXT(PCoffset9)], Setcc

    DR M[M[PC + SEXT(PCoffset9)]], Setcc

    DR M[BaseR + SEXT(offset6)], Setcc

    DR PC + SEXT(PCoffset9), Setcc

    DR NOT SR, Setcc

    M[PC + SEXT(PCoffset9)] SR

    M[M[PC + SEXT(PCoffset9)]] SR

    M[BaseR + SEXT(offset6)] SR

    STI SR, PCoffset9

    STR SR, BaseR, offset6

    NOTES: RTL corresponds to execution (after fetch!); JSRR not shown

    0000 trapvect8

    HCMUT-midterm.pdfLC3-with-RTL.pdf