装配

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指向同一个对象。我不知道现在该怎么办。。
我知道这和我的程序集代码有关。。但我不知道我做错了什么。这和这堆东西有关系吗?

最佳答案

更改:

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>

10-07 19:30
查看更多