PDA

Click to See Complete Forum and Search --> : Simple routine won't work


ca9mbu
Nov 20th, 2000, 10:41 AM
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



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

Sam Finch
Nov 20th, 2000, 11:01 AM
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.

parksie
Nov 20th, 2000, 04:54 PM
It looks like you keep zeroing ax, which is where C stores the return value.

Also, be wary of mixing 16bit and 32bit code.

Sam Finch
Nov 20th, 2000, 06:39 PM
IU don't think that's it, there's only 2 mov AX, 0 lines, one on entry and one on failure.

ca9mbu
Nov 21st, 2000, 05:56 AM
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