Assembly programming question

Started by
20 comments, last by TheUnbeliever 16 years ago
Well the question doesn't state whether address 0 needs to be kept original or not, so here's one that'll keep address 0 intact:

STO A,1
SUB A,1 ; A = 0

SUB A,0 ; A = -0
STO A,1
SUB A,1 ; A = 0
SUB A,1 ; A = +0
STO A,1

Edit: D'oh! Beaten by a minute! :)
Advertisement
Quote:Original post by prtc22
Quote:Original post by Halifax2
Where does the subtraction end up at when the value at the address is subtracted from the register. Is it in memory, or does the result get stored in the register.
It says "from the register" so the assumption is the result is stored in the register.

Here's my version which has 1 less instruction than some others:

STO A,1 // [1] = A
SUB A,1 // A = 0
SUB A,0 // A = -v (where v = original value at memory location 0)
STO A,1 // [1] = A = -v
SUB A,1 // A = -v - (-v) = 0
SUB A,1 // A = 0 - (-v) = v
STO A,1 // [1] = v

Quote:Original post by Halifax2
Quote:Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.
When he saying '-0' and '+0' in the comments he means the -/+ of the value originally stored in memory location 0, not actually the value 0.


Ah okay, my fault.

And by the way, which company gave you this test?
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume
Quote:Original post by samoth
This is what I can work out in a few mins... surely not optimal, but should do the job.

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = - m0
STO A, (0) // m0 = - m0

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = 0 - (-m0) = m0

STO A, (1)


Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]
Quote:Original post by Halifax2
Quote:Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.


I'm assuming SUB uses unsigned integral math, in which case "-0" is just my shorthand for the complement of the value in the 0 address, with respect to the memory size of 32 bits.

For example, if the memory size was 4 bits, the maximum representable value would be 15 (once again, assuming unsigned values). Just for kicks, assume the value in address 0 is 5 at the start. After zero-ing both registers, the last four commands are:

SUB A,0 // register A now has (0 - 5) % 15 => 10
STO A,0 // address 0 now has A's value of 10
SUB B,0 // register B now has (0 - 10) % 15 => 5
STO B,1 // address 1 now has B's value of 5
Quote:Original post by Bob_the_dev
Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]
Your second to last line doesn't actually do [A = 0-(-m0) = m0], it does [A = (-m0) - 0 = -m0], so the result you then store in memory location 1 is -m0.
Quote:Original post by Bob_the_dev
Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]

Edit: yah... lotsa editting in this thread...
Quote:Original post by lougv22
Quote:Original post by NerdInHisShoe
I came up with:

STO A, 1
SUB A, 0
SUB A, 1
STO A, 1
STO B, 1
SUB B, 0
STO B, 1
SUB A, 1
STO A, 1



Hmm, could you briefly explain what each line does.


Sorry, theres an error in my first solution, disregard it.

I did this on paper like this:

A C
B D

A, B are registers, C D are memory locations 0 and 1 respectively

The trick is to get -C in memory and 0 in a register to do 0 - (-C) = C, then move it to memory

STO A,1A CB ASUB A,0A-C CB   ASUB A,1-C  C B  ASTO A,1-C  C B  -CSTO B,0-C  B B -CSUB B,0-C  B 0 -CSUB B,1-C  B C -CSTO B,1-C  B C  C
Johnny was a chemist's son by Johnny is no more, for what Johnny thought was H2O was HO4
Quote:Original post by prtc22
Quote:Original post by Bob_the_dev
Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]
Your second to last line doesn't actually do [A = 0-(-m0) = m0], it does [A = (-m0) - 0 = -m0], so the result you then store in memory location 1 is -m0.


Ahh you're right. Bummer
Quote:Original post by Halifax2
Quote:Original post by prtc22
Quote:Original post by Halifax2
Where does the subtraction end up at when the value at the address is subtracted from the register. Is it in memory, or does the result get stored in the register.
It says "from the register" so the assumption is the result is stored in the register.

Here's my version which has 1 less instruction than some others:

STO A,1 // [1] = A
SUB A,1 // A = 0
SUB A,0 // A = -v (where v = original value at memory location 0)
STO A,1 // [1] = A = -v
SUB A,1 // A = -v - (-v) = 0
SUB A,1 // A = 0 - (-v) = v
STO A,1 // [1] = v

Quote:Original post by Halifax2
Quote:Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.
When he saying '-0' and '+0' in the comments he means the -/+ of the value originally stored in memory location 0, not actually the value 0.


Ah okay, my fault.

And by the way, which company gave you this test?


I think it was Budcat Creations.
I noticed many of you are only using register A in their solutions. It seems to me if register B wasn't needed they wouldn't give it to us. Can those who didn't utilize register B explain why?

This topic is closed to new replies.

Advertisement