Results 1 to 5 of 5

Thread: Simple routine won't work

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    44
    How is it that this sample of asm code won't work? It's the very first example in the book as well

    Imagine you have a 2d pyramid of blocks. Each row will have one more block than the row above it. This routine is supposed to calculate the number of blocks in the pyramid when given the number of blocks on the last row.

    It's designed to be called from Borland C++ using the syntax SUMS(X). It only ever returns 0.

    sums.asm

    Code:
        PUBLIC _sums
    
        .MODEL small
        .CODE
    
    _sums    PROC Value:WORD
             MOV  AX, 0         ;Initialise to zero
             MOV  CX, Value     ;Get actual value
             JCXZ S3            ;Num=0, no need to do
             CLC                ;Initialise for start
    S1:      ADC  AX, CX        ;Add row value
             JC   S2            ;Quit if AX overflowed
             LOOP S1            ;Repeat process
             JMP  S3            ;Successful completion
    S2:      MOV  AX, 0         ;Force a zeor
    S3:      RET                ;Return to C++
    _sums    ENDP
    
             ENDC
    The only thing I thought of is that CX is never decremented each time round the loop - hence given the value 7 at start it will continue adding 7 each time round the loop until eventaully AX is overflowed.

    Even adding the line SUB CX, 1 in that loop didn't work though. I'm using TASM that came with C++ Builder 5.5.

    Am I right in thinking that once this routine (and any other routine I write) has been called the reult will be placed in AX, or is this dependent on what I write?

    Sorry for so many questions, but as you can see I've only just started!

    Thanx, Matt
    VB5 Enterprise, C++Builder 5, JBuilder 3.5 (so far unused )

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I don't know much about TASM and C++ builder, but it could be the calling convention.

    Passing parameters to functions is a bit wierd, because there are so many ways of doing it, try changing the code slightly, just as a test, where you have

    MOV CX, Value

    use

    MOV CX, 4


    this means it ignores the parameter and just passses 4 to cx, see what happens then, it should return 10 in ax, if it does then you know that the MOV CX, Value line is wrong and it's not getting the parameter.


  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It looks like you keep zeroing ax, which is where C stores the return value.

    Also, be wary of mixing 16bit and 32bit code.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    IU don't think that's it, there's only 2 mov AX, 0 lines, one on entry and one on failure.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    44
    I've had to change the .asm code slightly to get the 32-bit stuff in (I just gave you the plain source to see if the error was in there!). I added the .486 directive and changed all the registers to E** (i.e. ax became eax).

    I then tested again without getting a value from C++ (i.e. the line mov cx, value became mov, cx 4) and this actually works. So the error lies in being able to pass the variable from C++ to asm reliably.

    Anyway, having sen it suggested in many threads in this forum I'm now using Pass32, and am slowly wading through his fairly thorough tutorial/manual.

    Thanx for the help

    Matt
    VB5 Enterprise, C++Builder 5, JBuilder 3.5 (so far unused )

Posting Permissions

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



Click Here to Expand Forum to Full Width