Quantcast
C/C++ help - Beyond.ca - Car Forums
Results 1 to 7 of 7

Thread: C/C++ help

  1. #1
    Join Date
    Nov 2003
    Location
    Vernon, BC
    My Ride
    2017 Golf TSI
    Posts
    2,463
    Rep Power
    23

    Default C/C++ help

    Stupid question, but I'm in the midst of translating some C code to assembler, and I'm stuck on one line.
    return bufp > 0 ? buf[--bufp] : getchar();

    Does this roughly translate to
    if (bufp > 0)
    buf[--bufp] = getchar();
    return buf[--bufp]
    ??
    I just don't want to translate it wrong, but I always get messed up with these stupid question marks in the code.

    If it's of any consequence, bufp is an int, and bufp is an array of char's.
    Originally posted by Vagabond142
    Is the best game. Ever. In everness. It is more awesome than a robot caveman punching God in the dick. It is that awesome

  2. #2
    Join Date
    Mar 2003
    Location
    Calgary
    Posts
    5,497
    Rep Power
    27

    Default

    Basically that just says:

    if (bufp> 0)
    return buf[--bufp];
    else
    return getchar();

    What assembly language are you using?

  3. #3
    Join Date
    Nov 2003
    Location
    Vernon, BC
    My Ride
    2017 Golf TSI
    Posts
    2,463
    Rep Power
    23

    Default

    Ahhh, good thing I asked, I did mis-read it.

    I'm programming in SPARC assembly.

    It's for a CPSC 355 assignment. That's one of the last bits holding me up. I seem to have a typo in another function that's giving me a bit of grief tracking it down.
    Originally posted by Vagabond142
    Is the best game. Ever. In everness. It is more awesome than a robot caveman punching God in the dick. It is that awesome

  4. #4
    Join Date
    Nov 2003
    Location
    Vernon, BC
    My Ride
    2017 Golf TSI
    Posts
    2,463
    Rep Power
    23

    Default

    Well, I thought I had a typo, but perhaps I'm just mis-reading something again.

    return val[sp++] = f;
    is that the same as:
    sp++;
    val[sp] = f;
    return val[sp];

    ??
    Or is that more like
    return val[sp];
    val[sp++] = f;
    ?? (I don't think so...)

    Or, the third choice:
    val[sp] = f;
    sp++;
    return f;

    While I'm asking, return val[--sp] does?....
    return val[sp]
    sp--
    ?
    sp--;
    return val[sp];
    ?

    I did it the third way... perhaps that's my problem?
    Originally posted by Vagabond142
    Is the best game. Ever. In everness. It is more awesome than a robot caveman punching God in the dick. It is that awesome

  5. #5
    Join Date
    May 2002
    Location
    Calgary, Alberta
    My Ride
    (maah raahde)
    Posts
    5,799
    Rep Power
    44

    Default

    Originally posted by Zero102

    return val[sp++] = f;
    It's essentially:

    val[sp] = f;, which is what is returned
    sp += 1;

    So your third choice would be the closest.


    While I'm asking, return val[--sp] does?....
    return val[sp]
    sp--
    ?
    sp--;
    return val[sp];
    ?

    sp -= 1;
    return val[sp];

    So your second one is the closest. Don't get your pre/post-increment/decrement's mixed up. It can cause you some grief if you dont' know the difference

  6. #6
    Join Date
    Nov 2003
    Location
    Vernon, BC
    My Ride
    2017 Golf TSI
    Posts
    2,463
    Rep Power
    23

    Default

    Would explain the stack over/underflows I've been getting.
    I was doing both the first way.
    I'll fix that up now.

    I know assembler, but not C, lol.
    Although I do know java pretty well, but I know it well enough to stay the hell away from pre/post increment/decrement's. I just actually add the number, lol.
    It all reduces to roughly the same machine code anyways, but my way is 10x easier to read to the untrained reader.

    Well, I'll fix this up, and if my program still is buggered up, I'll be back here with the next thing I may have mis-interpreted.
    Originally posted by Vagabond142
    Is the best game. Ever. In everness. It is more awesome than a robot caveman punching God in the dick. It is that awesome

  7. #7
    Join Date
    Nov 2003
    Location
    Vernon, BC
    My Ride
    2017 Golf TSI
    Posts
    2,463
    Rep Power
    23

    Default

    Ugh, bloody seg faults now..... if you guys know anything about SPARC assembly, you might be as confused as I am....

    mov 1, %o1
    set sp, %o0
    st [%o0], %o1

    and I get a seg fault....
    sp is an integer.

    yet, this works just fine
    set sp, %o0
    ld [%o0], %o1

    I can touch the same section of memory with no seg faults....

    I can even take it as far as...


    str: .asciz "%d\n"

    ....
    ....

    set sp, %o0
    ld [%o0], %o1
    set str, %o0
    call printf
    nop

    And I'll get the correct value for sp, but for some reason, I can't set the value of it....
    Originally posted by Vagabond142
    Is the best game. Ever. In everness. It is more awesome than a robot caveman punching God in the dick. It is that awesome

Similar Threads

  1. Help Me Quick! Asap!!! Help Help

    By Si1ent_A5sa5sin in forum Entertainment
    Replies: 6
    Latest Threads: 09-19-2003, 11:00 PM
  2. Help edmonton guys...help asap please

    By TomTom in forum General Car/Bike Talk
    Replies: 4
    Latest Threads: 07-20-2002, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •