# Choosing two lowest values in set of three

Page 1 of 1

## 3 Replies - 1390 Views - Last Post: 29 September 2009 - 12:50 PM

### #1 d_r

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 29-September 09

# Choosing two lowest values in set of three

Posted 29 September 2009 - 08:29 AM

I am a beginning computer science student. My college teaches Java as the first language and one of my first classes is teaching assembly language. I'm having trouble with an assignment that requires writing an assembly language program that will accept three integers and output the two lowest. I only have certain operations available to me (addition, subtraction, multiplication, division, jump if negative, jump if not negative, jump, and halt to stop the program).

So if I input x=8, y=-10 and z=9, I'd have to write a program that would output 8 and -10 using only basic arithmetic and the limited conditional programming provided by the jumps. These aren't hard numbers and they're entered at random so it could be any three integers, negative or non-negative.

After the integers are entered, I'd start by:

```load x
subtract y
jump if not negative to NEW_STEP (if the result is 0 or higher, I know y is smaller than or equal to x, so i will output y in the next step)
output y
NEW_STEP output x
subtract z
jump if not negative to NEW_STEP2
output z
halt program
NEW_STEP2 output y
halt program
```

The logic seems simple enough, but I'm missing an arithmetic or logical step somewhere because running the program will sometimes output the two lowest digits and sometimes it won't (it changes depending on the order of the numbers and/or if a negative digit is input first, second or third).

As is the way of these forums, I'm not looking for the answer...I really want to learn on my own. But any advice on what I might be overlooking would be appreciated.

Is This A Good Question/Topic? 0

## Replies To: Choosing two lowest values in set of three

### #2 wildgoose

• D.I.C Regular

Reputation: 67
• Posts: 468
• Joined: 29-June 09

## Re: Choosing two lowest values in set of three

Posted 29 September 2009 - 11:59 AM

"I am a beginning computer science student. My college teaches Java as the first language and one of my first classes is teaching assembly language."

Do you mean 'learning' assembly language?

You need to find out which assembler and libraries (if any) you are suppose to use!

Technically you'll need to input integer values (I assume from the keyboard) and store them in a register or memory. Once you have two, then do your mathematical operations and branch.
Note in asembly language there are two conditional branches

signed vs unsigned.
Comparisons, Addition, and Subtraction set similar flags, but the ones that count here are Overflow, Sign, and Zero flags.

Unsigned Branches
JA = JNBE Jump Above (Not Below or Equal) A > B
JAE = JNB Jump Above or Equal A >= B
JBE = JNA Jump Below or Equal A <= B
JB = JNAE Jump Below A < B

Signed Branches
JG = JNLE Jump Greater A > B
JGE = JNL Jump Greater or Equal A >= B
JLE = JNG Jump Less or Equal A <= B
JL = JNGE Jump Less A < B

The (=) between instructions are the same instruction, just a different name

For exampe je jz are the same Jump if Equal Jump if Zero
It's what makes sense when reading the code!

### #3 d_r

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 29-September 09

## Re: Choosing two lowest values in set of three

Posted 29 September 2009 - 12:31 PM

Thanks. I guess I should have been more clear. My professor crafted a small program to teach us how to work in an assembly language environment and we run it from the command line with our program in a text file. It's sort of like he created his own training assembly language, so we're not using any established language in particular and don't have access to all the commands/functions you just laid out. We can only do basic arithmetic (no >, <, =, if statements, etc.) Instructions can load data into the register and perform basic math on the data, and we can move to the next instruction or jump locations based on on the sign of the integer.

Thinking it through, I guess this question is more about logic and not assembly language in particular. My math or logic is off in the above program and I was just looking for insight into what I'm overlooking in my instructions or in the logic itself.

Again, I appreciate your help.

### #4 wildgoose

• D.I.C Regular

Reputation: 67
• Posts: 468
• Joined: 29-June 09

## Re: Choosing two lowest values in set of three

Posted 29 September 2009 - 12:50 PM

Never seen this before but here's my stab at it!

Scripting Language

```load x
subtract y

<-- Assuming X is on stack	X - y

jump if not negative to NEW_STEP (if the result is 0 or higher, I know y is smaller than or equal to x, so i will output y in the next step)

<--   x-y  means x <= y so y is same or bigger then x if here!
output y

NEW_STEP output x
subtract z
jump if not negative to NEW_STEP2

<--  (y <= z)?
so	Y - Z	thus   here if Y <= Z
output z

halt program
NEW_STEP2 output y
halt program

```

This post has been edited by wildgoose: 29 September 2009 - 12:50 PM