stonebreaker
asked on
Little Man Computer, How to multiply?
I am having trouble multiplying 2 numbers using Little Man Computer. I have read on here that other people have had the same problem. The answers didn't make sense to me because the commands in the version I am using are very different. Does anyone know how to multiply 2 numbers together using the following options?
STOP
LOAD
STORE
ADD
SUBTRACT
BRANCH
BRANCHZ
BRANCHP
STOREA
READ
PRINT
STOP
LOAD
STORE
ADD
SUBTRACT
BRANCH
BRANCHZ
BRANCHP
STOREA
READ
ASKER
Ok so given the options I have how would my commands go?
Using Branch, are you able to loop through a given condition.
Since you have three types of Branch statements, I believe atleast one of them should work for loop statements.
If you have loop then you can write a looping condition to achieve your output right.
Since you have three types of Branch statements, I believe atleast one of them should work for loop statements.
If you have loop then you can write a looping condition to achieve your output right.
Do you need to be able to handle one or two factors negative? Or at least equal to 0? If so, a few more subcases need to be considered...
ASKER
No only positive.
ASKER
I have found out that the BRANCHZ can be used as a loop. So given this information how would this go if you were to put all of the functions in order?
ASKER
Ok here is what I have so far but it's not working out so far. What am I doing wrong? HELP !!!!!!
Line# Instruction Address
00 READ
01 STORE 60
02 READ
03 STORE 61
04 BRANCHZ 60
05 PRINT
06 STOP
Line# Instruction Address
00 READ
01 STORE 60
02 READ
03 STORE 61
04 BRANCHZ 60
05 PRINT
06 STOP
Ok. Here's the logic:
1. READ and STORE first value as 60.
2. READ and STORE second value as 61.
3. STORE third value as 1.
4. BRANCHZ first value till third value equals first value.
5. ADD second value with second value and STORE.
6 ADD 1 to third value and BRANCHZ it.
Hope this solves. If you get any errors in logic give me the parameters accepted for those keywords so that I can think more deeper.
1. READ and STORE first value as 60.
2. READ and STORE second value as 61.
3. STORE third value as 1.
4. BRANCHZ first value till third value equals first value.
5. ADD second value with second value and STORE.
6 ADD 1 to third value and BRANCHZ it.
Hope this solves. If you get any errors in logic give me the parameters accepted for those keywords so that I can think more deeper.
ASKER
I tried this logic out but had problems.
I tried the following
READ
STORE 60
READ
STORE 61
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
ADD 61
STORE 62
ADD 62
BRANCHZ 62
PRINT
STOP
When it got to the first BRANCHZ I get the following error message Referring to address with no instruction
I tried the following
READ
STORE 60
READ
STORE 61
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
ADD 61
STORE 62
ADD 62
BRANCHZ 62
STOP
When it got to the first BRANCHZ I get the following error message Referring to address with no instruction
hiya... i did some 6502 stuff some years ago so this is pretty closely related. hope it's what you're looking for.
start // multiply two numbers
// written by paul tomasi 2/2008
// get two numbers
get1 READ // get number from user
BRANCHZ get1 // if it's '0' get another
STORE num1 // store as num1
get2 READ // same process for num2
BRANCHZ get2
STORE num2
// swap num1 with num2 to optimize for smallest countdown
SUBTRACT num1 // subtract num1 from num2
BRANCHP swap // if positive result num2 is greater than num1
// initialise count and running total (num2)
LOAD num2 // initialise count with smaller number
STORE count
LOAD #0 // reset running total
STORE num2
BRANCH loop // go and multiply
// swap num1 and num2 so num1 = largest number
swap LOAD num1 // initialise count with smallest number
STORE count
LOAD num2 // move largest number into num1
STORE num1
LOAD #0 // reset running total
STORE num2
loop LOAD num2 // add number to itself and store
ADD num1
STORE num2
LOAD count // countdown (using smaller number of the two)
SUBTRACT #1
BRANCHZ exit // if no more to do, exit loop otherwise...
STORE count // store count and repeat loop
BRANCH loop
exit LOAD num2 // get result (running total)
PRINT // output it to user
STOP // end
// data section
data num1
num2
count
here's a slightly modified version.
the code for reseting the running total need only appear once - as we can jump to it before multiplying. this is also neat because after the swap routine,we run into it anyway.
the code for reseting the running total need only appear once - as we can jump to it before multiplying. this is also neat because after the swap routine,we run into it anyway.
start // multiply two numbers
// written by paul tomasi 2/2008
// get two numbers
get1 READ // get number from user
BRANCHZ get1 // if it's '0' get another
STORE num1 // store as num1
get2 READ // same process for num2
BRANCHZ get2
STORE num2
// swap num1 with num2 to optimize for smallest countdown
SUBTRACT num1 // subtract num1 from num2
BRANCHP swap // if positive result num2 is greater than num1
// initialise count and running total (num2)
LOAD num2 // initialise count with smaller number
STORE count
BRANCH multiply // go and multiply
// swap num1 and num2 so num1 = largest number
swap LOAD num1 // initialise count with smallest number
STORE count
LOAD num2 // move largest number into num1
STORE num1
multiply LOAD #0 // reset running total
STORE num2
loop LOAD num2 // add number to itself and store
ADD num1
STORE num2
LOAD count // countdown (using smaller number of the two)
SUBTRACT #1
BRANCHZ exit // if no more to do, exit loop otherwise...
STORE count // store count and repeat loop
BRANCH loop
exit LOAD num2 // get result (running total)
PRINT // output it to user
STOP // end
// data section
data num1
num2
count
and if you're interested, i've included a MS-DOS batch file simulation which you can run from a DOS command line.
Copy and paste the code straight into notepad and save as MULTIPLY.BAT the just run it by typing:
MULTIPLY
at the DOS command prompt
PS. Runs in XP
Copy and paste the code straight into notepad and save as MULTIPLY.BAT the just run it by typing:
MULTIPLY
at the DOS command prompt
PS. Runs in XP
@rem multiply.bat
@rem written by paul tomasi
@rem simulates assembly code to multiply two numbers
@echo off
:start
:get1
set /p Acc=#1
if %Acc% EQU 0 goto :get1
set /a num1=%Acc%
:get2
set /p Acc=#2
if %Acc% EQU 0 goto :get2
set /a num2=%Acc%
set /a Acc-=%num1%
if %Acc% GTR 0 goto :swap
set /a Acc=%num2%
set /a count=%Acc%
set /a Acc=0
set /a num2=%Acc%
goto :loop
:swap
set /a Acc=%num1%
set /a count=%Acc%
set /a Acc=%num2%
set /a num1=%Acc%
set /a Acc=0
set /a num2=%Acc%
:loop
set /a Acc=%num2%
set /a Acc+=%num1%
set /a num2=%Acc%
set /a Acc=%count%
set /a Acc-=1
if %Acc% EQU 0 goto :result
set /a count=%Acc%
goto :loop
:result
set /a Acc=%num2%
echo Result [[%Acc%]]
:end
set num1=
set num2=
set count=
set Acc=
exit /b
Try this...The logic should be like
READ
STORE 60
READ
STORE 61
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
ADD 61
STORE 59 -- (60-1)
ADD 61
BRANCHZ 59
PRINT
STOP
READ
STORE 60
READ
STORE 61
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
BRANCHZ 60
ADD 61
STORE 59 -- (60-1)
ADD 61
BRANCHZ 59
STOP
Having revisited this question, I have looked deeper into LMC and found the following link which has a java-based LMC simulator.
http://www.atkinson.yorku.ca/~sychen/research/LMC/LMCHome.html
http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan.html
Looking at the small instruction set, I was able to write code after just a few moments. The following code is based on their instruction set however, I have also translated this using the instructions you have provided above.
INP
STA 16
INP
STA 17
LDA 17
BRZ 12
SUB 15
STA 17
LDA 18
ADD 16
STA 18
BRA 4
LDA 18
OUT
HLT
DAT 1
DAT 0
DAT 0
DAT 0
As you can see, there are four data locations: 15 is preset to the value '1' and is the value subtracted from our count during each iteration of the loop. 16 contains the first inputted number. 17 contains the second inputted number which acts as a countdown to zero. Finally, 18 is the running total where the result is stored during each iteration and from where we fetch our result at the end of the program.
This translates to the following machine code which can be entered directly into the LMC simulator. Then, by clicking the 'Step' button, you'll be able to see each instruction processed step-by-step.
901
316
901
317
517
712
215
317
518
116
318
604
518
902
000
1
0
0
0
Using the instruction set you have provided yourself, this would translate to:
READ
STOREA 16
READ
STOREA 17
LOAD 17
BRANCHZ 12
SUBTRACT 15
STOREA 17
LOAD 18
ADD 16
STOREA 18
BRANCH 4
LOAD 18
PRINT
STOP
1
0
0
0
Explanation:
The first four lines are straight forward.
READ - get 1st number into A (Accumulator)
STOREA 16 - store it in memory loaction 16
READ - get 2nd number into A
STOREA 17 - store is in memory location 17. This will be our countdown value
Next, we start our loop:
LOAD 17 - get the countdown value from memory location 17
BRANCHZ 12 - if it has reached zero then jump out of the loop, otherwise....
SUBTRACT 15 - subtract the number stored in location 15 (which is preset to '1'), and....
STOREA 17 - store the value back in memory location 17
That completes the conditional loop control
LOAD 18 - get our running total into A (first time loaded this will be zero)
ADD 16 - add the first number to it, and....
STOREA 18 - store it away again
So, this is basically adding the first number to itself for as many times euqal to the value of the second number
BRANCH 4 - go back to memory location 4 which is the start of our loop and repeat the process
And that completes the loop. Going back to our 6th instruction, this was a branch if zero to exit the loop, and this brings us here (memory location 12).
LOAD 18 - get our running total into A, and....
PRINT - display it as our result
Finally, we must terminate the program otherwise it will run into our data section and do some silly stuff.
STOP
And that's all there is to it.
The program starts at memory location 0 and occupies only 15 memory locations. The 16th memory location (15) is the start of our data. Memory location 15 is pre-set to '1' and is used to decrement our 'count' value. 16 is where our first number is stroed. 17 is where our second number is stored and is subsequently used as a countdown-to-zero to count how many times we should add the first number to itself (simple multiplication). 18 is where we store our running total - the result.
I hope this is clearer for you.
If you have further problems please let me know which LMC program your instructions belong to or provide a link to where you got your mnemonics from.
http://www.atkinson.yorku.ca/~sychen/research/LMC/LMCHome.html
http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan.html
Looking at the small instruction set, I was able to write code after just a few moments. The following code is based on their instruction set however, I have also translated this using the instructions you have provided above.
INP
STA 16
INP
STA 17
LDA 17
BRZ 12
SUB 15
STA 17
LDA 18
ADD 16
STA 18
BRA 4
LDA 18
OUT
HLT
DAT 1
DAT 0
DAT 0
DAT 0
As you can see, there are four data locations: 15 is preset to the value '1' and is the value subtracted from our count during each iteration of the loop. 16 contains the first inputted number. 17 contains the second inputted number which acts as a countdown to zero. Finally, 18 is the running total where the result is stored during each iteration and from where we fetch our result at the end of the program.
This translates to the following machine code which can be entered directly into the LMC simulator. Then, by clicking the 'Step' button, you'll be able to see each instruction processed step-by-step.
901
316
901
317
517
712
215
317
518
116
318
604
518
902
000
1
0
0
0
Using the instruction set you have provided yourself, this would translate to:
READ
STOREA 16
READ
STOREA 17
LOAD 17
BRANCHZ 12
SUBTRACT 15
STOREA 17
LOAD 18
ADD 16
STOREA 18
BRANCH 4
LOAD 18
STOP
1
0
0
0
Explanation:
The first four lines are straight forward.
READ - get 1st number into A (Accumulator)
STOREA 16 - store it in memory loaction 16
READ - get 2nd number into A
STOREA 17 - store is in memory location 17. This will be our countdown value
Next, we start our loop:
LOAD 17 - get the countdown value from memory location 17
BRANCHZ 12 - if it has reached zero then jump out of the loop, otherwise....
SUBTRACT 15 - subtract the number stored in location 15 (which is preset to '1'), and....
STOREA 17 - store the value back in memory location 17
That completes the conditional loop control
LOAD 18 - get our running total into A (first time loaded this will be zero)
ADD 16 - add the first number to it, and....
STOREA 18 - store it away again
So, this is basically adding the first number to itself for as many times euqal to the value of the second number
BRANCH 4 - go back to memory location 4 which is the start of our loop and repeat the process
And that completes the loop. Going back to our 6th instruction, this was a branch if zero to exit the loop, and this brings us here (memory location 12).
LOAD 18 - get our running total into A, and....
PRINT - display it as our result
Finally, we must terminate the program otherwise it will run into our data section and do some silly stuff.
STOP
And that's all there is to it.
The program starts at memory location 0 and occupies only 15 memory locations. The 16th memory location (15) is the start of our data. Memory location 15 is pre-set to '1' and is used to decrement our 'count' value. 16 is where our first number is stroed. 17 is where our second number is stored and is subsequently used as a countdown-to-zero to count how many times we should add the first number to itself (simple multiplication). 18 is where we store our running total - the result.
I hope this is clearer for you.
If you have further problems please let me know which LMC program your instructions belong to or provide a link to where you got your mnemonics from.
Just to make sure you got it....
00 READ
01 STOREA 16
02 READ
03 STOREA 17
04 LOAD 17
05 BRANCHZ 12
06 SUBTRACT 15
07 STOREA 17
08 LOAD 18
09 ADD 16
10 STOREA 18
11 BRANCH 4
12 LOAD 18
13 PRINT
14 STOP
15 1
16 0
17 0
18 0
00 READ
01 STOREA 16
02 READ
03 STOREA 17
04 LOAD 17
05 BRANCHZ 12
06 SUBTRACT 15
07 STOREA 17
08 LOAD 18
09 ADD 16
10 STOREA 18
11 BRANCH 4
12 LOAD 18
13 PRINT
14 STOP
15 1
16 0
17 0
18 0
ASKER
t0t0 this explanation is great and I think is getting close. I tried this out and I get the following error message when I get to the 7th step down. Also LMC didn't like it the first time I tried using STOREA so I used STORE instead. That got me to the SUBTRACT 15. This is when I received the following error message.
Error: Instruction subtract, "garbage loaded on address 15"
Error: Instruction subtract, "garbage loaded on address 15"
ASKER
How do I preset address 15 to 1? Maybe this is why I get the error message.
Check out the looping logic in the same site as mentioned earlier.
http://www.atkinson.yorku.ca/~sychen/research/LMC/LMCLoops.html
There are some more additional commands present in Little computer it seems as per the site. Try using those too.
http://www.atkinson.yorku.ca/~sychen/research/LMC/LMCLoops.html
There are some more additional commands present in Little computer it seems as per the site. Try using those too.
Just type a '1' in location 15.
Could you tell me what program / simulator / emuolator you're using so that I can synchronise with you.
If it's the LMC I gave you a link to earlier then just type a '1' in location 15. Also, type a '0' in locations 16, 17 and 18 as well - although these will probably be zero to start with anyway.
I wasn't sure about your STORE and STOREA but because LMC appears to operate on the Accumulator I assumed we should use STOREA.
Could you tell me what program / simulator / emuolator you're using so that I can synchronise with you.
If it's the LMC I gave you a link to earlier then just type a '1' in location 15. Also, type a '0' in locations 16, 17 and 18 as well - although these will probably be zero to start with anyway.
I wasn't sure about your STORE and STOREA but because LMC appears to operate on the Accumulator I assumed we should use STOREA.
rrjegan17
the link you posted above is unrelated to this question as the loop employed in this application need only count down to zero so we're anly testing for zero.
when you load a value into the Accumulator a single instruction ie, BRZ is enough to perform the loop's conditional statement whereas, in the example you provided a 'count' value is loaded then '10' is subtracted from it and then tested to see if the result is positive - a bit messy for our needs.
the link you posted above is unrelated to this question as the loop employed in this application need only count down to zero so we're anly testing for zero.
when you load a value into the Accumulator a single instruction ie, BRZ is enough to perform the loop's conditional statement whereas, in the example you provided a 'count' value is loaded then '10' is subtracted from it and then tested to see if the result is positive - a bit messy for our needs.
ASKER
t0t0 it is Assembler Visual Builder version 1.0.2. I have attached a screen shot.
LMC.jpg
LMC.jpg
Ok.. Thanks for the snapshot.
I havent used it or seen it earlier.
I havent used it or seen it earlier.
stonebreaker
Sorry, I've been out for the day in the snow with my little one.
Right, I have two solutions for you which I will discuss in detail as soon as I have completed my write-up.
In the meantime, if you want to crack on with one of my solutions have a look at the following code. This WILL need some explanation so that you understand sometimes not everything is done using conventional methods because in assembly language, we can get away with the 'unconventional'.
Using the (rather uninspiring) LMC interpretter (of your choice), enter the code and click the 'Compile' button. Before stepping through the program, grab the right-hand scrollbar and drag it down so that you can see the memory range 36..39. When stepping thrpigh the program, you can observe the values changing (most notably 36 and 37). The result is displayed in the 'Output Stream' wndow.
00 LOAD 09 = 109
01 SUBTRACT 13 = 413
02 STORE 39 = 239
03 READ = 901
04 STORE 38 = 238
05 READ = 901
06 STORE 37 = 237
07 LOAD 19 = 119
08 STORE 36 = 236
09 LOAD 37 = 137
10 BRANCHZ 17 = 617
11 SUBTRACT 39 = 439
12 STORE 37 = 237
13 LOAD 36 = 136
14 ADD 38 = 338
15 STORE 36 = 236
16 BRANCH 09 = 509
17 LOAD 36 = 136
18 PRINT = 902
19 STOP = 000
Sorry, I've been out for the day in the snow with my little one.
Right, I have two solutions for you which I will discuss in detail as soon as I have completed my write-up.
In the meantime, if you want to crack on with one of my solutions have a look at the following code. This WILL need some explanation so that you understand sometimes not everything is done using conventional methods because in assembly language, we can get away with the 'unconventional'.
Using the (rather uninspiring) LMC interpretter (of your choice), enter the code and click the 'Compile' button. Before stepping through the program, grab the right-hand scrollbar and drag it down so that you can see the memory range 36..39. When stepping thrpigh the program, you can observe the values changing (most notably 36 and 37). The result is displayed in the 'Output Stream' wndow.
00 LOAD 09 = 109
01 SUBTRACT 13 = 413
02 STORE 39 = 239
03 READ = 901
04 STORE 38 = 238
05 READ = 901
06 STORE 37 = 237
07 LOAD 19 = 119
08 STORE 36 = 236
09 LOAD 37 = 137
10 BRANCHZ 17 = 617
11 SUBTRACT 39 = 439
12 STORE 37 = 237
13 LOAD 36 = 136
14 ADD 38 = 338
15 STORE 36 = 236
16 BRANCH 09 = 509
17 LOAD 36 = 136
18 PRINT = 902
19 STOP = 000
ASKER
You have done it!!!!!!!!!!!!!!!!!!!!!!!! !!!!!! Thank you so much for all of your help. I look forward to your explanation so that I can learn and understand it better. Once again thank you so much.
My explanation is almost complete.... I will post it in about 30 minutes or so.... please bear with me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
stonebreaker
Thank you for accepting my solution. I don't know what your knowledge of assembly programming is but I wonder, did I make things clear enough for you to understand what was going on?
Thank you for accepting my solution. I don't know what your knowledge of assembly programming is but I wonder, did I make things clear enough for you to understand what was going on?
Follow the approach given below:
1. Branch it out.
2. Load first no.
3. Load second no.
4. Compare second no from 1 till its value.
5. End Branch once done.
Hope this logic helps you.