多次调用后,从C调用汇编代码擦除地址

Tha*_*yen 0 c assembly

部件

TITLE   adding numbers
.486
.MODEL      FLAT,C
option      casemap:none
include     C:\Users\Thao\Hello\msvcrt.inc
.data
formating  DB "%s",10,0
           DB 0
num1       DB "0000000000"
           DB 0

num2       DB "0000000000"
           DB 0

AC_balance DB "0000000000"
           DB 0


.code

adding      PROC C, number1:DWORD , number2:DWORD

            mov esi, number1
            lea edi, num1 
            mov ecx, SIZEOF num2
            rep movsb 
            mov     eax, offset num1

            push    eax

            mov     eax, OFFSET formating
            push    eax
            call    printf
            pop     eax
            pop     eax
;---------------------------------------------------            

            mov esi, number2
            lea edi, num2 
            mov ecx, SIZEOF num2
            rep movsb 

            mov     eax, OFFSET num2
            push    eax

            mov     eax, OFFSET formating
            push    eax
            call    printf
            pop     eax
            pop     eax


            mov esi,SIZEOF num1-1
            mov edi,SIZEOF num1-1
            mov ecx,SIZEOF num1
            mov bh,0            ;used to save carry value


            L1: mov ah,0            ;clear AH before the add
            mov al,num1[esi]    ;get a digit
            add al,bh               ;add previous carry (0 first time through)
            aaa                     ; adjust the total
            mov bh,ah               ; save the carry in BH
            or  bh,30h              ;convert carry to ASCII character

            add al, num2[esi]   ;perform main add
            aaa                     ; adjust the main add, AH gets the carry
            or  bh,ah               ; combine the carries
            or  bh,30h              ; convert back to ASCII
            or  al,30h              ; convert digit back to ascii
            mov num2[edi],al    ;save total digit

            dec     esi             ;move left 1 digit in addends
            dec     edi             ;move left 1 digit in result
            loop    L1

            mov eax, offset num2
            push eax

            mov eax, offset formating
            push eax
            call printf
            pop eax
            pop eax

            mov eax, offset num2

            ret
adding      ENDP
END 
Run Code Online (Sandbox Code Playgroud)

C代码

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

char *adding(char *y,char *x);
void _tmain(void)
{
    char *a, *b, *a1, *b1;
    int z = 6;
    int sizeA =9;
    int sizeB =9;
    a = (char*)malloc(11*sizeof(char));
    b = (char*)malloc(11*sizeof(char));
    a1 = (char*)malloc(11*sizeof(char));
    b1 = (char*)malloc(11*sizeof(char));
    strcpy(a, "0000000000");
    strcpy(b, "0000000001");
    strcpy(a1, "3234567890");   
    strcpy(b1, "1234567890");

    printf("adding A\n");
    a=adding(a1,a);
    printf("a: %s\n",a);
    printf("adding B\n");
    b=adding(b1,b);
    printf("a: %s\n",a);
    printf("b: %s\n",b);
    getchar();
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT目前

adding A
3234567890
0000000000
a: 3234567890
adding B
1234567890
0000000001
a: 1234567891
b: 1234567891
Run Code Online (Sandbox Code Playgroud)

好吧,我现在处于路障状态,因为某些原因我正在掏出地址,当我在视觉工作室调试时,我注意到地址似乎写了我猜.而且我不确定为什么以及如何写出来.在运行结束时,A和B指向同一件事.我现在还不确定该怎么做..

我知道它与我的汇编代码有关...但我不确定我做错了什么.它与堆栈有关吗?

Pau*_*l R 5

更改:

a = (char*)malloc(10*sizeof(char));
b = (char*)malloc(10*sizeof(char));
a = "0000000000";
b = "0000000001";
Run Code Online (Sandbox Code Playgroud)

至:

a = malloc(11);
b = malloc(11);
strcpy(a, "0000000000");
strcpy(b, "0000000001");
Run Code Online (Sandbox Code Playgroud)

(你还需要添加#include <string.h>)

  • 您需要根据您使用的任何ABI保存和恢复寄存器.我猜这是Windows,所以你需要遵循非易失性寄存器的Windows ABI调用约定. (3认同)
  • 好吧,你至少应该修复这个部分,因为目前你正在分配内存,然后改变指针指向只读字符串文字(让malloc'd内存孤立),这既无意义又可能有错误. (2认同)