组装
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>
)
关于从 C 调用汇编代码在多次调用后删除地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10287808/