1

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

1 Answers1

0

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).
Ped7g
  • 16,236
  • 3
  • 26
  • 63