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
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
好吧,我现在处于路障状态,因为某些原因我正在掏出地址,当我在视觉工作室调试时,我注意到地址似乎是我写的。而且我不确定为什么以及如何写出来。在运行结束时,A和B指向同一件事。我现在还不确定该怎么办..
我知道它与我的汇编代码有关...但我不确定我做错了什么。它与堆栈有关吗?
答案 0 :(得分: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>
)