Question

tried returning a long int from asm proc through eax, and later tried through dx:ax. both didn't work for me as the C printf prints a different number than the required 320L.

x.asm:

.model SMALL
.stack 100h
.code
.386
; extern int get_buffered_long(long int **arr, int num_of_elements, int i);
;                                   [BP+4]      [BP+6]              [BP+8]
PUBLIC _get_bufferred_long 
_get_bufferred_long PROC NEAR
        push BP
        mov BP,SP
        push SI
        push DI
        push BX
        call locate_slot            
        ;mov EAX, DWORD PTR [DI]     ;something here doesn't work. the way i return a long int to borland C, or other issue.
        mov ax,[DI]
        mov dx,[DI+2]
        pop BX
        pop DI
        pop SI
        pop BP
        ret
_get_bufferred_long ENDP
; extern void set_bufferred_long(long int *arr[], int buff_size, int i,long int value);
;                                       [BP+4]      [BP+6]      [BP+8]      [BP+10]
PUBLIC _set_bufferred_long
_set_bufferred_long PROC NEAR
    push BP
    mov BP,SP
    pushad
    call locate_slot
    mov EAX,[BP+10]
    mov DWORD PTR [DI],EAX
    popad
    pop BP
    ret
_set_bufferred_long ENDP
; helper function that makes DI point to the slot wanted.
locate_slot PROC NEAR
    calc_slot:
        mov SI,[BP+4]
        mov AX,[BP+8]
        mov BX,[BP+6]
        xor DX,DX
        div BX
    locate_slot_in_array:
        shl AX,1
        add SI,AX
        mov DI,[SI]
        shl DX,2
        add DI,DX
        ret
locate_slot ENDP
end

y.c:

#include "stdio.h"
#include "stdlib.h"

extern int get_bufferred_long(long int**,int,int);
extern void set_bufferred_long(long int**,int,int,long int);

int main(){
    long int **arr;
    int i,j,n,m;
    n = 5;
    m = 4;
    arr=(long int**)malloc(n*sizeof(long int*));
    for(i=0; i < n; i = i + 2) arr[i] = malloc( m*sizeof(long int));
    for(i=1; i < n; i = i + 2) arr[i] = malloc( m*sizeof(long int));
    for(i=0; i < n; i++)
        for(j=0; j < m; j++) 
            set_bufferred_long(arr, m, i*m+j, i*100+j*10);

    printf("get_bufferred_long(arr, %d, %d) = %ld\n", m, 14, get_bufferred_long(arr,m, 14));
    return 0;
}

the set function works (array looks exactly like needed to). the get function works too, it fetches 320L in asm, but when passed to C, something's wrong.

no compile errors or warnings. borland c++ 5.02

Was it helpful?

Solution

Well, on a 386 bcc does use AX:DX in 16-bit mode; don't know about 32 bits.

But look at your code!

    ...
    mov dx,[DI+2]
    mov ax,[DI]
    mov dx,[DI+2]
    pop DX
    ...

You are loading the DX register with the result and then popping the stack into it, blowing away the value it had. DX does not have to be saved by push/pop in a simple procedure (only e.g. in a DOS ISR).

EDIT

Okay I see you fixed the problem above in the code. The next problem is likely to be that you're declaring

/* After execution, return value is assumed to be in AX. */ 
extern int get_bufferred_long(long int**,int,int);

and then expecting a 32-bit return value. You mentioned that the printf is pushing the AX register. This implies you're compiling to 16-bit code. If you want a 32-bit return value in 16-bit code, you must declare the return value long and place it in DX:AX.

/* After execution, return value is assumed to be in DX:AX reg pair. */
extern long get_bufferred_long(long int**,int,int);

You can verify the right return convention by compiling a tiny program to assembly with the -S option. Try for example:

long val(void) { return 0x12345678L; }

Look at the generated assembly to see what the compiler does to return this long value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top