# 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```

LDA B

STA 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

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
STA RESULT   // Store the new RESULT
LDA COUNT    // Load 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
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
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
{"ft_lang_label":"__label__en","ft_lang_prob":0.86405057,"math_prob":0.8253645,"size":1097,"snap":"2020-45-2020-50","text_gpt3_token_len":234,"char_repetition_ratio":0.17200366,"word_repetition_ratio":0.049723756,"special_character_ratio":0.2898815,"punctuation_ratio":0.14220184,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9767725,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-22T11:33:11Z\",\"WARC-Record-ID\":\"<urn:uuid:ba212e59-2962-48b5-b339-ff04f33ecc3e>\",\"Content-Length\":\"138567\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b6608f1a-3981-4582-a1ff-b2c3d243ed50>\",\"WARC-Concurrent-To\":\"<urn:uuid:5856c614-93ae-4519-80fb-6732b99537f0>\",\"WARC-IP-Address\":\"104.27.129.139\",\"WARC-Target-URI\":\"https://www.testandtrack.io/index.php/studenttest/givetest/551\",\"WARC-Payload-Digest\":\"sha1:UYMWECIT6DSHWP5U6DVWTBFWOAWB2XWE\",\"WARC-Block-Digest\":\"sha1:NOPULDYEVJA4HLCGNECKHBVCND3X47UJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107879537.28_warc_CC-MAIN-20201022111909-20201022141909-00610.warc.gz\"}"}