INP
STA FIRST
INP
STA SECOND
INP
STA NUMBERONE
BRZ SUBTRACTION
LDA FIRST
SUB SECOND
OUT
INP
STA NUMBERTWO
(LOOP)
OUT
HLT
INP
STA FIRST
INP
STA SECOND
LDA FIRST
OUT
LDA SECOND
OUT
STA ONE_SUB
HLT
FIRST DAT
SECOND DAT
ONE_SUB DAT
INP
STA FIRST
INP
STA SECOND
LDA FIRST
OUT
LDA SECOND
OUT
INP
STA ONE_SUB
HLT
FIRST DAT
SECOND DAT
ONE_SUB DAT
INP
STA FIRST
INP
STA SECOND
LDA FIRST
OUT
LDA SECOND
OUT
INP
STA ONEORTWO
SUB ONE
BRZ NRONE
LDA FIRST
ADD SECOND
OUT
LDA ONEORTWO
SUB TWO
BRZ NRTWO
LDA FIRST
STA COUNT
LOOPTOP LDA COUNT
ADD SECOND
OUT
STA COUNT
SUB SECOND
BRP ENDLOOP
BRA LOOPTOP
ENDLOOP HLT
FIRST DAT
SECOND DAT
ONEORTWO DAT
COUNT DAT
INP
STA FIRST
INP
STA SECOND
LDA FIRST
OUT
LDA SECOND
OUT
INP
STA ONEORTWO
SUB ONE
BRZ NRONE
LDA FIRST
ADD SECOND
OUT
LDA ONEORTWO
SUB TWO
BRZ NRTWO
LDA FIRST
STA COUNT
LOOPTOP LDA COUNT
ADD SECOND
OUT
STA COUNT
SUB SECOND
BRP ENDLOOP
BRA LOOPTOP
ENDLOOP HLT
FIRST DAT
SECOND DAT
ONEORTWO DAT
ONE DAT
TWO DAT
NRONE DAT
NRTWO DAT
COUNT DAT
GetInput INP
STA First
INP
STA Second
GetOption INP
BRZ End
SUB One
BRZ Addition
SUB One
BRZ Multiply
BRA GetOption
Addition LDA First
ADD Second
OUT
BRA End
Multiply LDA First
ADD Second
STA Count
End HLT
First DAT
Second DAT
One DAT 1
Count DAT
.
START


 
 Get FIRST 
 




 
 Get SECOND 
 


>
 
 
  
  Get Option 
  
 
 
 
 / \
 / \
 / \
 / Is \ Yes
 [ Option ]
 \ '0' / 
 \ / 
 \ / 
 \ / 
  No 
  
 / \ 
 / \ 
 / \ 
 / Is \ Yes 
 [ Option ] 
 \ '1' /  
 \ /  
 \ /  
 \ /  
  No  
   
 / \  
 / \  
 / \  
 / Is \ Yes  
 [ Option ] 
 \ '2' /   
 \ /   
 \ /   
 \ /   
    
 No   
  
  
  
   
    
 Addition <  
    
   
  
  
  
   
    
  Multiply < 
   
  
  
> 
 
 
  
Display Result  
  
 
 
<


 
 End 
 

.
GetInput INP
STA First
INP
STA Second
GetOption INP
BRZ End
SUB One
BRZ Addition
SUB One
BRZ Multiply
BRA GetOption
Addition LDA First
ADD Second
STA Sum
BRA DisplayResult
Multiply
DisplayResult LDA Sum
OUT
End HLT
First DAT
Second DAT
Sum DAT
One DAT 1
Count DAT
.
Multiply


 
 Set Result=0 
 




 
Set Count=First
 


>
 
 / \
 / \
 / \
 / Is \ Yes
 [ Count ]> DisplayResult
 \ '0' /
 \ /
 \ /
 \ /
  No
 
 
  
  Add Second to 
  Result 
  
 
 
 
 
  
  Decrement 
  Count 
  
 
 

.
.
Multiply


 
 Set Result=0 
 




 
Set Count=First
 


>
 
 / \ You need to branch to that section of code...
 / \ ...which will process the result.
 / \
 / Is \ Yes
 [ Count ]> DisplayResult
 \ '0' /
 \ /
 \ /
 \ /
  No
 
 
   You need to load one of our numbers we input previously.
  Add Second to  You need to add the other number to it.
  Result  You need to strore the result away so that...
   ...it can be processed in a later section.
 
 
 
 
  
  Decrement 
  Count 
  
 
 

.
GetInput INP
STA First
INP
STA Second
GetOption INP
BRZ End
SUB One
BRZ Addition
SUB One
BRZ Multiply
BRA GetOption
Addition LDA First
ADD Second
STA Result
BRA DisplayRes
Multiply LDA a
ADD b
STA a
OUT
HLT
LDA a
ADD First
STA Count
OUT
LOOPTOP LDA Count
SUB One
STA Count
BRZ DisplayRes
LDA First
ADD SECOND
STA Count
BRZ LOOPTOP
DisplayRes LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT 1
One DAT 1
Count DAT
a DAT 0
b DAT 1
.
Multiply <Pt.1: "Initialise Result" to go here (2 lines of code)>
<Pt.2: To discuss and to go here later,
but program will still work without it>
<Pt.3> ADD First <
STA Count  Good start
Loop LDA Count <
SUB One < Must be careful here because
STA Count Count might already be '0'
BRZ DisplayResult < Good intention
LDA First <
ADD SECOND  No good
STA Count <
BRZ Loop < Good intention
DisplayResult LDA Result < Great!!
OUT
End HLT < Great!!
First DAT
Second DAT
Result DAT
One DAT 1 < Only one needing initialising.
Count DAT
Please look at the last flowchart describing the operation of this section of code. And give it a little more thought please.
.
Multiply LDA ResultMul
STA Count
ADD First
STA Count
Loop LDA Count
*
*
*
*
DisplayRes LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT
One DAT 1
ResultMul DAT 0
Count DAT
.
Multiply

 (2 lines ie,)
 
(Label)  Set Result=0  LDA Zero
Miltiply   STA Result



 (2 lines)
 
Set Count=First
 


>
 
 / \
 / \ (1 line)
 / \
 / Is \ Yes
(Label)  [ Count ]> DisplayResult
????  \ '0' /
 \ /
 \ /
 \ /
  No
 
  (3 lines)
  
  Add Second to 
  Result 
  
 
 
 
  (3 lines)
  
  Decrement 
  Count 
  
 
 
 (1 line)
Multiply LDA Zero
STA Result
ADD First
STA Count
Loop LDA Count
SUB One
BRZ DisplayRes
LDA Result
ADD Second
STA Result
*
*
BRZ Loop
DisplayRes LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT
One DAT 1
Zero DAT 0
Count DAT
GetInput INP
STA First
INP
STA Second
GetOption INP
BRZ End
SUB One
BRZ Addition
SUB One
BRZ Multiply
BRA GetOption
Addition LDA First
ADD Second
STA Result
BRA ShowResult
Multiply LDA Zero
STA Result
LDA First
STA Counter
Loop BRZ ShowResult
LDA Result
ADD Second
STA Result
LDA Counter
SUB One
STA Counter
BRZ Loop
ShowResult LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT
One DAT 1
Zero DAT 0
Counter DAT
Multiply


  LDA Zero
 Set Result=0  STA Result
 




  LDA First
Set Count=First STA Count
 


(Loop) >
 
 / \
 / \
 / \ BRZ DisplayResult
 / Is \ Yes
 [ Count ]> DisplayResult
 \ '0' /
 \ /
 \ /
 \ /
  No
 
 
   LDA Result
  Add Second to  ADD Second
  Result  STA Result
  
 
 
 
 
   LDA Count
  Decrement  SUB One
  Count  STA Count
  
 
 
 BRA Loop
So, your code should look like the following:
Multiply LDA Zero
STA Result
LDA First
STA Counter
Loop BRZ ShowResult
LDA Result
ADD Second
STA Result
LDA Counter
SUB One
STA Counter
BRA Loop
.
GetInput INP
STA First
INP
STA Second
GetOption INP
BRZ End
SUB One
BRZ Addition
SUB One
BRZ Multiply
BRA GetOption
Addition LDA First
ADD Second
STA Result
BRA ShowResult
Multiply LDA Zero
STA Result
LDA First
STA Count
Loop BRZ ShowResult
LDA Result
ADD Second
STA Result
LDA Count
SUB One
STA Count
BRA Loop
ShowResult LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT
One DAT 1
Zero DAT 0
Count DAT
.


/ \ 3 lines of code
/is \
/First\
/smaller\ Yes
[ than ]
\Second?/ 
\ / 
\ / 
\ / 
 No 
 
Swap   6 lines of code
  
 Swap First  
 and Second  
  
 
 
NoSwap <

.
Multiply LDA Zero
STA Result
LDA Second
SUB One
BRP NoSwap
Swap LDA First
STA Count
LDA Second
STA First
NoSwap LDA Second
STA Count
LDA First
STA Count
Loop BRZ ShowResult
LDA Result
ADD Second
STA Result
LDA Count
SUB One
STA Count
BRA Loop
ShowResult LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT
One DAT 1
Zero DAT 0
Count DAT
.


/ \ (3 lines of code)
/is \
/First\ Get Second
/smaller\ Yes Subtract First
[ than ] Branch if result is positive
\Second?/ 
\ / 
\ / 
\ / 
 No 
 
  6 lines of code
  
 Swap First  
 and Second  
  
 
 
<

.
Multiply LDA Zero < Initialise Result
STA Result
LDA Second < Test First is smaller than Second
SUB First
BRP NoSwap < Skip the Swap routine
Swap LDA First < Entry point for Swap
STA Temp 
LDA Second 
STA First 
LDA Temp 
STA Second <
NoSwap LDA First < Initialise loop counter
STA Count
Loop LDA Count < Test if counter is '0'
BRZ ShowResult < If it is, exit loop
LDA Result < Add Second to running total
ADD Second 
STA Result <
LDA Count < Decrement loop counter
SUB One 
STA Count <
BRA Loop < Repeat loop
.
GetInput INP
STA First
INP
STA Second
GetOption INP
BRZ End
SUB One
BRZ Addition
SUB One
BRZ Multiply
BRA GetOption
Addition LDA First
ADD Second
STA Result
BRA ShowResult
Multiply LDA Zero
STA Result
LDA Second
SUB First
BRP NoSwap
Swap LDA First
STA Temp
LDA Second
STA First
LDA Temp
STA Second
NoSwap LDA First
STA Count
Loop LDA Count
BRZ ShowResult
LDA Result
ADD Second
STA Result
LDA Count
SUB One
STA Count
BRA Loop
ShowResult LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT
One DAT 1
Zero DAT 0
Count DAT
Temp DAT
.
GetInput INP
STA First
INP
STA Second
GetOption INP
BRZ End
SUB One
BRZ Addition
SUB One
BRZ Multiply
BRA GetOption
Addition LDA First
ADD Second
OUT
HLT
Multiply LDA Second
SUB First
BRP NoSwap
Swap LDA Second
STA Result
LDA First
STA Second
LDA Result
STA First
NoSwap LDA End
STA Result
Loop LDA First
BRZ ShowResult
SUB One
STA First
LDA Result
ADD Second
STA Result
BRA Loop
ShowResult LDA Result
OUT
End HLT
First DAT
Second DAT
Result DAT
One DAT 1
.
By clicking you are agreeing to Experts Exchange's Terms of Use.
Title  # Comments  Views  Activity 

Patching a Windows EXE  23  727  
Swapping a Int64 var (using ASM)  19  1,090  
core dump analysis  26  2,563  
How to make a Bootable Program without an OS  4  516 
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
10 Experts available now in Live!