tryingToLearnC
tryingToLearnC

Reputation: 33

print odd numbers in MARIE assembly language up to user input value

It is a homework and I'm stuck here. Any help is appreciated.

I'm trying to print odd numbers up to a user input value (say 6 or 7). I have the following code which kind of does what I want but not exactly.

Here is my code:

org 100

input             /ask for input
store num         /store the input as num

load one
store oddnum      /store 1 as odd number
output            /print odd number, prints 1

oddloop, load oddnum  /start of loop
add two               /adds 2 in previous odd number value
store oddnum          /stores the new odd number
output                /prints odd number

load num              /loads user input
subt oddnum           /input value minus current odd number value

skipcond 000          /skips next line if value is negative
jump oddloop          /jumps to loop

halt                  /end program

zero,   dec 0
one,    dec 1
two,    dec 2
num,    dec 0
oddnum, dec 0

if the user input is 7; it prints

1 3 5 7 9

here, expected output is 1 3 5 7

if the user input is 6; it prints

1 3 5 7

here, expected output is 1 3 5

Upvotes: 2

Views: 2291

Answers (2)

trincot
trincot

Reputation: 350310

The problem is that you check whether a number exceeded the limit after you have already printed it. You should do this in opposite order: first check if you should exit the loop, and if not, only then output the current odd number.

Secondly, you probably also want this to work also when the user enters 0 as num: in that case you shouldn't print anything. That means you should not print before the loop, and when the loop starts you should start with the limit verification.

Here is your code with those corrections:

          input
          add   one       / To help the exit condition
          store num
          load  one
          store oddnum

oddloop,  subt  num       / first perform the check
          skipcond 000
          halt
          load  oddnum          
          output          / then output
          add   two       / and then increment
          store oddnum
          jump  oddloop

one,      dec 1
two,      dec 2
num,      dec 0
oddnum,   dec 0

Upvotes: 1

Ped7g
Ped7g

Reputation: 16596

change logic of your code, there are many possible ways, but I would personally do this one:

num = input
oddnum = 1

while (oddnum <= num) {
  output oddnum
  oddnum += 2
}

(oddnum <= num) is equal to (not(oddnum > num))

So in other words, if (oddnum-num) > 0 is true, then jump to first instruction after loop (exiting it), otherwise loopy loop till looped into looblivion...


And why I would use this one logic and not some other:

  • notice there's only single output point (you have two)
  • it's while(){}, not do {} while(), so it will work also for "0" input (not displaying anything), or "1" ("2") input displaying only "1" (the do {} while() logic with two outputs will display at least "1 3" every time).

Upvotes: 1

Related Questions