Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

how can you access an write to specific cells in array of pointers in assembly in model large?

i need to write a program in c that creates a variable long int **arr , and then allocates to each cell a partition sized m like this:

arr = malloc(n*sizeof(long int *));

for(i=0; i < n;i=i+2 )

arr[i] = malloc( m*sizeof(long int));

and then create an asm program in model large that implement the function:

extern void set_bufferred_long(long int *arr[], int buff_size, int i,long int value); -which sets cells of the array

this is my implantation of the function in asm:

.MODEL LARGE

.CODE

PUBLIC _set_bufferred_long ;set_bufferred_long(long int *arr[], int buff_size, int i,long int value)

; [bp+6] [bp+10] [bp+12] [bp+14]

_set_bufferred_long PROC far

PUSH BP ;Preserve BP register

MOV BP,SP

PUSH SI ;Preserve SI register

PUSH DI ;Preserve DI register

PUSH ES ;Preserve ES register

MOV AX,[BP+12] ;i

MOV CX,[BP+10] ;m

DIV CX ;AX=I/M DX=I%M

MOV CX,2

MOV BX,DX ;BX=I%M

MUL CX ;AX=AX*2 =(I/M)*2

MOV ES,[BP+8]

MOV DI,[BP+6] ;address of arr

ADD DI,AX ;address of a[i] is the block inside the array

MOV SI,ES:[DI] ;[di]=the address of the memory where the block starts (each block contains m long int cells)

MOV AX,BX ;AX=I%M

MOV BX,4 ;each cell is long int=4

MUL BX

ADD SI,AX

MOV AX,[BP+14] ;first part of long int

MOV ES:[SI],AX

MOV AX,[BP+16] ;second part of long int

MOV ES:[SI+2],AX

POP ES ;Restore ES register

POP DI ;Restore DI register

POP SI ;Restore SI register

POP BP ;Restore BP register

RET

_set_bufferred_long ENDP

but while the program runs and compile the values are not placed correctly in the array, when i debug i see that when i try to get the address in a[i] (with the command MOV SI,ES:[DI]) ,si only gets the offset of the array ,and not the content of the cell (which should be the address of the partition i want to write to)

what am i doing wrong? (when i do the same program in model small with the proper changes it works fine)

any help would be appreciated

Update:

this is how set_bufferred_long is defined in c:

extern void set_bufferred_long(long int *arr[], int buff_size, int i,long int value);

and this is how i call it:

for(i=0; i < n; i++) {

for(j=0; j < m; j++) {

set_bufferred_long(arr, m, i*m+j,i*100+j*10);

}

}

Update 2:

buff_size =m (the size of each buffer/partition)

i=index to write to in the array , e.g i=10

means that i need to access index 10/m in the array , take the address from the cell arr[10/m] (because it is model long we need to use both offset and segment pointers) ,then add to the address +(10%m*4)

because each cell is long int

1 Answer

Relevance
  • 8 years ago
    Favorite Answer

    Show an example of the call from C with . Is buff_size the value n? And i the value m? As in:

    set_buffered_long( arr, n, m, 6 );

    ??

    arr[0....n-1] contains pointers to arrays of long int. arr[0][0...m-1] might be one such long array. So I'm assuming the function needs both n and m. But in which order are you passing them?

Still have questions? Get your answers by asking now.