Chaz Meyers (cpm) wrote,
Chaz Meyers

Hrmph. Victory, but no victory.

Hmm. I got my lab for 72 to work, but rather than figure out what the problem was, I took an entirely different route and that is annoying me.

The problem was to reverse an array in place using assembler, creating a function that looks like:
revarray( int *a, int n); // a is an array of n elements

My initial logic was:
Calculate address of end of array and store. Basically end = a + (n-1)*4, since c ints are 4 bytes.
loop: Load int at beginning and end of array into 2 registers.
Store in opposite locations.
Increment the register storing the beginning of the array by 4, effectively chopping off the first element.
Deincrement the register storing the end of the array by 4, effectively chopping off the last element.
If the address of the begining is less than the address of the end, branch back to loop.

For some reason, whenever it would go through the loop the second time, it would give an access violation. I narrowed it down to something being fishy with the line:
S4SUBL R1, R24, R24 ; r24 is end of array register, r1 is 1.

As I wrote it up right here, I just thought of something. See, I know that:

s#add r1, r2, r3

does r3 = r1*# + r2. But in adding, order doesn't matter. I'm wondering if s#sub subtract do:
r3 = r1*# - r2 or
r3 = r2 - r1*#

Hrmph. I really need it to do the latter, but that would explain why it didn't work. The wording in the manual is fuzzy:
"The first operand is multiplied by # and subtracted to the second operand"
What the hell is "subtracted to"? Hrmph.

Anyway, I solved the problem by changing n and moving the first bit calculating the end into the loop and looping until the counter is less than or equal to 0. I was going to point out that I was really annoyed that I had to change my solution to get it to work, since I don't know what I did wrong.. now I'm wondering if I just misunderstood the manual. Hmm. I guess I'm going to need to learn to use the bloody debugger soon. Can't rely on commenting out random lines until you find what breaks.

Matrix multiplication will be interesting, since I always forget how to do that by hand. :-p

  • My tweets

    Tue, 19:54: RT @ TheParkerJam: Suppose you are: * in rails in development mode * using cookies for sessions * raising an exception and not…

  • My tweets

    Mon, 12:48: Well, that was an unexpected turn.

  • My tweets

    Fri, 20:07: I made my first comment on a YouTube video. It got 132 upvotes and 28 comments. The clout is definitely already going to my head.

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.