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! :)
Assembly programming question
Quote:Original post by prtc22Quote:Original post by Halifax2It says "from the register" so the assumption is the result is stored in the register.
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.
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] = vQuote:Original post by Halifax2When 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.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.
Ah okay, my fault.
And by the way, which company gave you this test?
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 Halifax2Quote: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_devYour 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.
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 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 lougv22Quote: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
Quote:Original post by prtc22Quote:Original post by Bob_the_devYour 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.
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]
Ahh you're right. Bummer
Quote:Original post by Halifax2Quote:Original post by prtc22Quote:Original post by Halifax2It says "from the register" so the assumption is the result is stored in the register.
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.
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] = vQuote:Original post by Halifax2When 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.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.
Ah okay, my fault.
And by the way, which company gave you this test?
I think it was Budcat Creations.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement