Question

The edit before the publish: So I kinda pretty much figured it all out on my own only AFTER I had the whole freakin' question typed out and neatly arranged. So as not to work for nothing, I'm posting this anyways, asking if this is actually right, and asking for any suggestions/comments. I'm still not sure about one thing.

Alright, so I am a complete noob to computer science. Right now I am learning about assembly-language and machine language programming. This is a homework question so please help me understand, don't throw answers in my face. I am trying to write a program that gets the sum of the numbers from 1 to 20 (the answer is 210) using branch on condition (a loop).

The Mnemonics used are:

L  for load  
A  for Add  
St for store  
BC for branch on condition  
C  for compare
CH for channel

There are 10 registers to put stuff into.

So basically I have to do 2 things: 1) Figure out a way to add up all numbers from 1 to 20 (1+2+3+4+5+6+7+8+9+...+20) which includes making a loop, AND 2) create a counter so it knows to stop when it gets to twenty. Is this right??

START    L  R1,COUNT
         A  R1,ONE
         ST R1,COUNT
         A  R2,COUNT
         C  R1,TWENTY
         BC  3,???
         ST R2,SUM
         CH SUM
         STOP
COUNT    DC F'0'
ONE      DC F'1'
TWENTY   DC F'20'
         END START

All this is saying: Load Register1 with contents of COUNT (which to begin with is 0). Add to Register1 contents of ONE (which is 1). Store this new number that's in Register1 into COUNT. Add this new number to Register2. Compare contents of COUNT with contents of TWENTY (which is 20). Branch (/repeat) if the number inside COUNT is less than or equal to 20 (3 is the condition code). Once the counter reaches 20, the condition will be broken and the instruction will go to the next line which is: Store the number which is in Register2 (which should now be 210) into variable SUM. Then call CHannel to display contents of SUM on I/O device. Then STOP.

Now... I don't know how to specify to branch/return back up to the beginning, so it repeats the process until the counter reaches 20. I'm also not sure if I should specify COUNT as 0 or not.

Thanks for any/all help.

Was it helpful?

Solution

A couple of things that I see. Since I have not tested this, I suggest that you test it yourself.

You will need to initialize Register 2 so that as you add the value of Count, Register 2 starts with a known value.

Typically a branch on condition requires that you specify the condition as well as a label to branch to if the condition is met. Not sure what your assembler requires for this.

So I would consider the following changes. I have put comments on each line using two slashes similar to C. I assume that the Branch on Condition instruction has two operands, a condition operand that indicates which flags to test and an offset which the assembler calculates to a label. And I assume the AD instruction will set the condition flags which the BC instruction can then test. I put a 3 for the conditional flags to test for the BC instruction however I am not sure what the operand should actually be so I put a comment on that line.

If your assembler allows adding two registers and has an XOR instruction allowing a register as an operand, you could just do away with the variable COUNT by doing XOR Register2 with itself and Register1 with itself to zero them out and to then do an ADD using the two registers. The XOR works because a one bit XORed with a one bit turns into a zero while a zero XORed with a zero stays a zero.

The Branch on Condition, BC, instruction may or may not allow a backward branch. If it does not or if there is some kind of a constraint on it, you may have to have to use a jump or JMP instruction to go back to the top of the loop with a BC instruction before the jump to branch over the JMP once the COUNT reaches 20.

START    L  R2,COUNT    // initialize Register 2 to zero as Count starts with zero
         L  R1,COUNT    // initialize Register 1 to zero as Count starts with zero
LOOP     A  R1,ONE      // add one as part of incrementing Count for the next sequence
         ST R1,COUNT    // save current Count back.
         A  R2,COUNT    // add the current value of Count into Register 2 for the sum
         C  R1,TWENTY   // compare Register 1, Count, to end of loop condition
         BC 3,LOOP      // branch if less than back to loop.  if 20 we are done.
         ST R2,SUM
         CH SUM
         STOP
COUNT    DC F'0'
ONE      DC F'1'
TWENTY   DC F'20'
SUM      DC F'0'
         END START
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top