2

私の仕事は、c の malloc を使用し、asm の関数へのポインターからそれに値を挿入するアセンブリ ルーチンを作成することでした。 "

c ファイル:

#include <stdio.h>
#include <stdlib.h>

extern  int initarr (int** arr, int n, int (*initfunc)());

int getNum() 
{return (rand()%1000);}

void main()
{
int * arr, i, n, success;
printf("\nPlease enter the array size\n");
scanf("%d",&n);
success = initarr(&arr, n, getNum);   
if(!success) 
{
    printf("Memory Allocation Failed\n");
    return;
} 
printf("\nThe Numbers in the allocated array are:\n");
for(i=0; i<n; i++)   
    printf("%d  ", arr[i]);
} 

asm ファイル:

;lab10.asm
.MODEL SMALL
.CODE
.386
;extern  int initarr (int** arr, int n, int (*initfunc)())
;arr=BP+4   n=BP+6  function=BP+8
EXTRN _malloc : NEAR
PUBLIC _initarr
_initarr PROC NEAR 
PUSH BP;preserve BP
MOV BP,SP;let me see whats in the stack
PUSH SI;preserve SI
MOV SI,[BP+4];SI controls array
MOV CX,[BP+6];CX=n from declaration
MOV eAX,8;size of one byte=8
MUL CX;how many bytes to allocate
SUB eSP,eAX;size in bytes to allocate
PUSH eAX;preserve for later add to sp
PUSH eCX;for ussing malloc
CALL _malloc;return addres is in eAX
TEST eAX,eAX;checks if allocation succeded
POP eCX;pop CX for the loop
POP eAX;pop AX to add to SP
ADD eSP,eAX;for stack won't change
JZ BYE;if allocation went wrong and eAX is already 0
MOV [SI],eAX;sets SI to point at the array
MOV SI,[SI];set SI to the array
LOOPER:
PUSH CX;for ussing rand
CALL [BP+8];call function
POP CX;pop CX for the loop
MOV [SI],eAX
ADD SI,2
LOOP LOOPER
;end of loop    
MOV eAX,1;for the return value
BYE:
POP SI;restore SI
POP BP;restore BP
RET
_initarr ENDP
END 

ここに画像の説明を入力

__________________編集___________________

5より大きいすべてのサイズで、その作業は素晴らしい ここに画像の説明を入力

4

1 に答える 1

2

「ヌル ポインター割り当て」メッセージは、MSDOS プラットフォームでコードをクリーンアップする一部のコンパイラーによって表示されます。クリーンアップ コードは、NULL ptr 周辺のメモリ領域の内容が変更されたかどうかをチェックします (MSDOS にはアクセス違反などがないため)。したがって、コードが NULL またはその他の低オフセットを逆参照できる場所を確認する必要があります。以下のソースでは、それを行うエラーを見つけることができます:

CALL _malloc;return addres is in eAX
TEST eAX,eAX;checks if allocation succeded
POP eCX;pop CX for the loop
POP eAX;pop AX to add to SP
ADD eSP,eAX;for stack won't change

_malloc は eAX で ptr を返し、2 コマンド後に POP eAX (8 を含む) によって上書きされます。後でそれを逆参照すると、MOV [SI],eAXそれがエラー メッセージの根本的な原因になります。

于 2012-12-27T23:27:13.400 に答える