LMC assembly language help Two integer numbers are entered b
LMC assembly language help
Two integer numbers are entered by the user through keyboard.
The two numbers are multiplied.
The multiplication result is printed on the console screen
This is what I have so far:
Solution
Multiplication is a type of addition. 5*8= 8+8+8+8+8
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.
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
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.
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


