组装TASM大模型过程(从C调用)矩阵循环

时间:2019-01-21 12:49:50

标签: assembly x86-16 tasm

在程序集8086中编写一个过程,该过程将从C接收以下信息:

int *matrix[size], int size, int *num1, int *num2. 

我首先将其写为小型模型,但效果很好,当我将其转换为大型模型时,它停止了正常工作,我尝试更改循环计数,将“行指针”加4,将“列指针”加2(都尝试加4,反之)。

该过程应检查最小值(绝对值),即最小值| x-y |。 例如,该过程将获得N个数字,并将其从所有其他数字中减去,并始终保存| x-y |的最小值。然后,它将N个数字向前移动一个数字,并与其余数字相同,直到所有矩阵都被选中为止。

C代码(此代码不能更改任何内容):

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

    extern int getDiffMin(int *mat[], int size , int *num1 , int *num2);

        int main()
        {
            int 

diff , i, j , num1 , num2;
        int **mat = (int **)malloc(sizeof(int *) * 4); 
        for (i = 0; i < 4; i++)
            mat[i] = (int *)malloc(sizeof(int) * 4);
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++)
                mat[i][j] = (i*j) - (5 * i) - (14 * j);
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 4; j++)
                printf("%d\t", mat[i][j]);
            printf("\n");
        }
        diff = getDiffMin(mat, 4 , &num1 , &num2);
        printf("\nminDiff == |(%d)-(%d)| = %d", num1 , num2 , diff);
        for (i = 0; i < 4; i++)
            free(mat[i]);
        free(mat);
        return 0;
    }

组装代码:

;extern int getDiffMin(int *mat[], int size , int *num1 , int *num2)
;                      BP+6+8      BP+10      BP+12+14    BP+16+18
;
.MODEL LARGE ;Pointers size = 4 bytes
.DATA
    I       DW  ?
    J       DW  ?
    N       DW  ?
    Min     DW  ?
    TempI   DW  ?
    TempJ   DW  ?
    Counter DW  ?
    Two     DW  2
.CODE
.386
PUBLIC _getDiffMin
_getDiffMin PROC FAR
Start:
    PUSH BP ;Save BP's current value
    MOV BP,SP
    PUSH SI
    PUSH DI
    PUSH CX
    PUSH ES
    PUSH GS
    MOV CX,WORD PTR [BP+10] ;CX point to size (matrix of size*size)
    MOV SI,WORD PTR [BP+6] ;SI point to pointer in *mat[] (points to rows)
    MOV ES,WORD PTR [BP+8] ;Segment of *mat[]
    MOV DI,WORD PTR ES:[SI] ;DI point to value in *mat[] (points to columns, with first pointer on rows so gives value)
    MOV ES,WORD PTR ES:[SI+2] ;Segment of *mat[]
    MOV I,0 ;Rows index counter
    MOV J,0 ;Columns index counter
    MOV Counter,CX
    ADD Counter,CX ;Counter for loop
FirstGetN: ;Get number to check with all other numbers
    MOV AX,WORD PTR ES:[DI] ;AX has value which DI is pointing to
    MOV N,AX
FirstSubNumbers:
    ADD DI,2 ;Point to next column
    MOV AX,WORD PTR ES:[DI] ;AX has value one column ahead from N
    SUB AX,N
    CMP AX,0
    JG Continue1
    NEG AX ;Convert sub result to positive
Continue1:
    MOV Min,AX ;Save minimum difference in Min for loop with compare ahead
    MOV SI,WORD PTR [BP+12] ;SI point to *num1 (in case first check had the minimum difference)
    MOV GS,WORD PTR [BP+14] ;Segment of *num1
    MOV AX,N
    MOV GS:[SI],AX ;*num1 = N
    MOV SI,WORD PTR [BP+16] ;SI point to *num2
    MOV GS,WORD PTR [BP+18] ;Segment of *num2
    MOV AX,WORD PTR ES:[DI] ;AX = second number
    MOV GS:[SI],AX ;*num2 = second number
GetN:
    MOV SI,WORD PTR [BP+6] 
    MOV ES,WORD PTR [BP+8] 
    ADD SI,I ;Add needed row
    MOV DI,WORD PTR ES:[SI]
    MOV ES,WORD PTR ES:[SI+2]
    ADD DI,J ;Add needed column
    MOV AX,WORD PTR ES:[DI]
    MOV N,AX ;N contains number being checked with others
    MOV AX,I ;In order to move from N location forward (numbers before were checked during the loop)
    MOV TempI,AX
    MOV AX,J
    MOV TempJ,AX
    MOV AX,Counter
    CMP TempJ,AX ;Meaning it's in last column
    JE Fix
AddJ:
    ADD TempJ,2 ;Move to next column
    JMP SubNumbers
Fix:
    MOV TempJ,0 ;Back to first column
    ADD TempI,4 ;New row
SubNumbers:
    MOV SI,WORD PTR [BP+6] 
    MOV ES,WORD PTR [BP+8] 
    ADD SI,TempI
    MOV DI,WORD PTR ES:[SI]
    MOV ES,WORD PTR ES:[SI+2]
    ADD DI,TempJ
    MOV AX,WORD PTR ES:[DI] ;AX contains number to check with N
    MOV BX,N ;Move N to BX in order to save N's value
    SUB BX,AX ;BX (N) -= AX
    CMP BX,0
    JG Continue2
    Neg BX
Continue2:
    MOV AX,Min
    SUB AX,BX ;Substract former minimum and current sub result
    CMP AX,0 ;To check which one is bigger
    JLE Continue3 ;If BX > AX 
    MOV Min,BX ;Else, Min=BX
    MOV SI,WORD PTR [BP+12] ;SI point to *num1
    MOV GS,WORD PTR [BP+14] ;Segment of *num1
    MOV AX,N
    MOV GS:[SI],AX ;*num1 = N
    MOV SI,WORD PTR [BP+16] ;SI point to *num2
    MOV GS,WORD PTR [BP+18] ;Segment of *num2
    MOV AX,WORD PTR ES:[DI] ;AX = second number
    MOV GS:[SI],AX ;*num2 = second number
Continue3:
    MOV AX,Counter
    CMP TempJ,AX ;Meaning all columns were checked 
    JNE AddJ
    MOV TempJ,0 ;Else, go to first column
    ADD TempI,4 ;Next row
    MUL Two
    CMP TempI,AX ;Meaning all rows (and columns) were checked 
    JNE SubNumbers
    MOV AX,Counter
    ADD J,2 ;Move to next column with N number (J is pointing to INT so advance 2 bytes)
    CMP J,AX
    JNE GetN
Fix2:
    MOV J,0
    MOV AX,Counter
    MUL Two
    ADD I,4
    CMP I,AX ;If all rows have been checked with N number (I is pointing to pointer so jumps of 4 byte, LARGE MODEL)
    JE Finish
    JMP GetN
Finish:
    MOV AX,Min ;Save value to return
    POP GS ;POP by LIFO order
    POP ES
    POP CX 
    POP DI
    POP SI
    POP BP
    RET ;AX, IP and CS
_getDiffMin ENDP
END Start

谢谢。

1 个答案:

答案 0 :(得分:0)

答案是我需要为“循环”添加2,因为“大小”(从C代码中获取)从0开始计数。

这是ASM代码:

;extern int getDiffMin(int *mat[], int size , int *num1 , int *num2)
;                      BP+6+8      BP+10      BP+12+14    BP+16+18
;
.MODEL LARGE ;Pointers size = 4 bytes
.DATA
    I       DW  ?
    J       DW  ?
    N       DW  ?
    Min     DW  ?
    TempI   DW  ?
    TempJ   DW  ?
    Counter DW  ?
    Two     DW  2
.CODE
.386
PUBLIC _getDiffMin
_getDiffMin PROC FAR
Start:
    PUSH BP ;Save BP's current value
    MOV BP,SP
    PUSH SI
    PUSH DI
    PUSH CX
    PUSH ES
    PUSH GS
    MOV CX,WORD PTR [BP+10] ;CX point to size (matrix of size*size)
    MOV SI,WORD PTR [BP+6] ;SI point to pointer in *mat[] (points to rows)
    MOV ES,WORD PTR [BP+8] ;Segment of *mat[]
    MOV DI,WORD PTR ES:[SI] ;DI point to value in *mat[] (points to columns, with first pointer on rows so gives value)
    MOV ES,WORD PTR ES:[SI+2] ;Segment of *mat[]
    MOV I,0 ;Rows index counter
    MOV J,0 ;Columns index counter
    MOV Counter,CX
    ADD Counter,2 ;Since size starts from 0, need to make our "loop" bigger by 2
FirstGetN: ;Get number to check with all other numbers
    MOV AX,WORD PTR ES:[DI] ;AX has value which DI is pointing to
    MOV N,AX
FirstSubNumbers:
    ADD DI,2 ;Point to next column
    MOV AX,WORD PTR ES:[DI] ;AX has value one column ahead from N
    SUB AX,N
    CMP AX,0
    JG Continue1
    NEG AX ;Convert sub result to positive
Continue1:
    MOV Min,AX ;Save minimum difference in Min for loop with compare ahead
    MOV SI,WORD PTR [BP+12] ;SI point to *num1 (in case first check had the minimum difference)
    MOV GS,WORD PTR [BP+14] ;Segment of *num1
    MOV AX,N
    MOV GS:[SI],AX ;*num1 = N
    MOV SI,WORD PTR [BP+16] ;SI point to *num2
    MOV GS,WORD PTR [BP+18] ;Segment of *num2
    MOV AX,WORD PTR ES:[DI] ;AX = second number
    MOV GS:[SI],AX ;*num2 = second number
GetN:
    MOV SI,WORD PTR [BP+6] 
    MOV ES,WORD PTR [BP+8] 
    ADD SI,I ;Add needed row
    MOV DI,WORD PTR ES:[SI]
    MOV ES,WORD PTR ES:[SI+2]
    ADD DI,J ;Add needed column
    MOV AX,WORD PTR ES:[DI]
    MOV N,AX ;N contains number being checked with others
    MOV AX,I ;In order to move from N location forward (numbers before were checked during the loop)
    MOV TempI,AX
    MOV AX,J
    MOV TempJ,AX
    MOV AX,Counter
    CMP TempJ,AX ;Meaning it's in last column
    JE Fix
AddJ:
    ADD TempJ,2 ;Move to next column
    JMP SubNumbers
Fix:
    MOV TempJ,0 ;Back to first column
    ADD TempI,4 ;New row
SubNumbers:
    MOV SI,WORD PTR [BP+6] 
    MOV ES,WORD PTR [BP+8] 
    ADD SI,TempI
    MOV DI,WORD PTR ES:[SI]
    MOV ES,WORD PTR ES:[SI+2]
    ADD DI,TempJ
    MOV AX,WORD PTR ES:[DI] ;AX contains number to check with N
    MOV BX,N ;Move N to BX in order to save N's value
    SUB BX,AX ;BX (N) -= AX
    CMP BX,0
    JG Continue2
    Neg BX
Continue2:
    MOV AX,Min
    SUB AX,BX ;Substract former minimum and current sub result
    CMP AX,0 ;To check which one is bigger
    JLE Continue3 ;If BX > AX 
    MOV Min,BX ;Else, Min=BX
    MOV SI,WORD PTR [BP+12] ;SI point to *num1
    MOV GS,WORD PTR [BP+14] ;Segment of *num1
    MOV AX,N
    MOV GS:[SI],AX ;*num1 = N
    MOV SI,WORD PTR [BP+16] ;SI point to *num2
    MOV GS,WORD PTR [BP+18] ;Segment of *num2
    MOV AX,WORD PTR ES:[DI] ;AX = second number
    MOV GS:[SI],AX ;*num2 = second number
Continue3:
    MOV AX,Counter
    CMP TempJ,AX ;Meaning all columns were checked 
    JNE AddJ
    MOV TempJ,0 ;Else, go to first column
    ADD TempI,4 ;Next row
    MOV AX,Counter
    ADD AX,2
    MUL Two
    CMP TempI,AX ;Meaning all rows (and columns) were checked 
    JNE SubNumbers
    MOV AX,Counter
    ADD J,2 ;Move to next column with N number (J is pointing to INT so advance 2 bytes)
    CMP J,AX
    JNE GetN
Fix2:
    MOV J,0
    ADD I,4
    MOV AX,Counter
    ADD AX,2
    MUL Two
    CMP I,AX ;If all rows have been checked with N number (I is pointing to pointer so jumps of 4 byte, LARGE MODEL)
    JE Finish
    JMP GetN
Finish:
    MOV AX,Min ;Save value to return
    POP GS ;POP by LIFO order
    POP ES
    POP CX 
    POP DI
    POP SI
    POP BP
    RET ;AX, IP and CS
_getDiffMin ENDP
END Start