0

部件

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 

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();
}

当前输出

adding A
3234567890
0000000000
a: 3234567890
adding B
1234567890
0000000001
a: 1234567891
b: 1234567891

好吧,由于某种原因,我现在处于一个路障,我正在整理地址,当我在 Visual Studio 中进行调试时,我注意到地址似乎被我猜想写了。而且我不确定它为什么以及如何被覆盖。到运行结束时,A 和 B 指向同一个东西。我现在完全不知道该怎么做..

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

4

1 回答 1

5

改变:

a = (char*)malloc(10*sizeof(char));
b = (char*)malloc(10*sizeof(char));
a = "0000000000";
b = "0000000001";

至:

a = malloc(11);
b = malloc(11);
strcpy(a, "0000000000");
strcpy(b, "0000000001");

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

于 2012-04-23T20:44:33.000 回答