Preview

04 - LMC #2

 1. Assembly language is ideal for situations in which it would be more efficient not to have a _____________ and for use in embedded systems (e.g.washing machine)

  embedder

  compiler

  assembler

  programmer

 2. As specified by the von Neumann architecture, each mailbox in LMC (signifying a unique memory location) contains both _______________________.

  instructions and data

  instruction sets and comparison operators

  branch instructions and loops

  ROM and RAM

 3. This code should add two numbers together and then output the sum. Can you fill in the blanks with the most appropriate command?
INP 
STA A 
INP 
STA B 
LDA A 
_____?
OUT 
HLT
A    DAT
B    DAT

  ADD A

  LDA B

  STA B

  ADD B

 4. Fill in the blanks for the last instruction. How is it different from BRZ?
 BRA - branch always.
 BRZ - branch if the outcome is 0.
 BRP - ???

  branch if the outcome is positive or zero.

  None of these answers are valid

  branch if the outcome is less than zero (i.e negative)

  branch if the outcome is text and not a number (i.e. zero integer input)

 5. Here is some LMC code for multiplication. Fill in the blanks with the most appropriate command.
INP
STA FIRST
INP
STA SECOND
LOOP    LDA SECOND
BRZ ENDTHIS
SUB ONE
STA SECOND
LDA ANS
________???
STA ANS
BRA LOOP
ENDTHIS   LDA ANS
OUT
SUB ANS
STA ANS
HLT
FIRST   DAT 
SECOND   DAT 
ONE    DAT 1
ANS   DAT 0

  LDA FIRST

  ADD FIRST

  ADD SECOND

  STA FIRST

 6. This code should check if 2 values are the same and output them if they are, else 0 if they are different. Which line is incorrect?
LDA NUMA
SUB NUMB
BRP LABEL
LDA ZERO
BRA STOP
LABEL 	LDA NUMA
STOP 		OUT
HLT
NUMA		DAT 4
NUMB		DAT 4
ZERO		DAT 0

  BRA STOP should be BRZ STOP

  BRP LABEL should be LOOP

  LDA ZERO should be LDA OUT

  BRP LABEL should be BRZ LABEL

 7. What is the purpose of the following code?
     INP
     OUT      // Initialize output 
LOOP BRZ QUIT // If the accumulator value is 0, jump to the memory address labeled QUIT
     SUB ONE  // Label this memory address as LOOP, 
              //The instruction will then subtract the value stored at address ONE from the accumulator
     OUT
     BRA LOOP // Jump (unconditionally) to the memory address labeled LOOP
QUIT HLT      // Label this memory address as QUIT
ONE  DAT 1    // Store the value 1 in this memory address, and label it ONE

  It will take a user input and count down to zero

  It will take a single input, store it, take another input, and compare both to see which is greater than 1

  it will store the value '1' in a memory address and compare the input to see if it is the same

  It will take mutiple user inputs and add them together using branch loops

 8. The following code will take user input, square it, output the answer and then repeat. Entering a zero into the program _______________________________.
START   LDA ZERO     // Initialize for multiple program run
        STA RESULT
        STA COUNT
        INP          // User provided input
        BRZ END      // Branch to program END if input = 0
        STA VALUE    // Store input as VALUE
LOOP    LDA RESULT   // Load the RESULT
        ADD VALUE    // Add VALUE, the user provided input, to RESULT
        STA RESULT   // Store the new RESULT
        LDA COUNT    // Load the COUNT
        ADD ONE      // Add ONE to the COUNT
        STA COUNT    // Store the new COUNT
        SUB VALUE    // Subtract the user provided input VALUE from COUNT
        BRZ ENDLOOP  // If zero (VALUE has been added to RESULT by VALUE times), branch to ENDLOOP
        BRA LOOP     // Branch to LOOP to continue adding VALUE to RESULT
ENDLOOP LDA RESULT   // Load RESULT
        OUT          // Output RESULT
        BRA START    // Branch to the START to initialize and get another input VALUE
END     HLT          // HALT - a zero was entered so done!
RESULT  DAT          // Computed result (defaults to 0)
COUNT   DAT          // Counter (defaults to 0)
ONE     DAT 1        // Constant, value of 1
VALUE   DAT          // User provided input, the value to be squared (defaults to 0)
ZERO    DAT          // Constant, value of 0 (defaults to 0)

  will cause the program to repeat

  will cause the program to execute the branch instruction and go down a different path

  will end the program

  will cause the program to reset and start from the beginning

 9. The following code demonstrates division. Fill in the blanks with the command that best fits.
INP M 
STA M
INP N
STA N
LOOP   LDA M
BRZ END
SUB N
STA M
LDA ANS
ADD ONE
STA ANS
BRA LOOP
END   LDA ANS
OUT
_______?????
STA ANS
HLT
M    DAT
N    DAT
ANS    DAT 0
ONE   DAT 1

  SUB ANS

  SUB N

  DIV ANS

  STA N

 10. This is the code for ___________________.
INP 
STA x
LDA x
STA y
LOOP LDA y
BRZ END
SUB ONE
STA y
LDA ANS
ADD x
STA ANS
BRA LOOP
END LDA ANS
OUT
SUB ANS
STA ANS 
HLT
x DAT
y DAT
ONE DAT 1
ANS DAT 0

  squaring a number

  taking an input and multiplying that input by itself

  taking an input and dividing it by itself

  adding two numbers and then multiplying the sum by itself