内联汇编跪求解答
输入字符串,转化大小写。
原代码是这样的:
#include <stdio.h>
int main()
{
char string[128];
gets(string);
_asm {
jmp entry
isletter1:
cmp al, 'a'
jl nex
cmp al, 'z'
jg nex
mov ecx, 1
ret
isletter2:
cmp al, 'A'
jl nex
cmp al, 'Z'
jg nex
mov ecx, 1
ret
nex:
mov ecx, 0
ret
entry:
lea edx, string
loopStart:
mov al, byte ptr ds:[edx]
test al, al
jz end
call isletter1
test ecx, ecx
jz test2
sub byte ptr ds:[edx], 'a'-'A'
jmp nextChar
test2:
call isletter2
test ecx, ecx
jz nextChar
add byte ptr ds:[edx], 'a'-'A'
nextChar:
inc edx
jmp loopStart
end:
}
puts(string);
return 0;
}
可以运行,但是我需要用汇编语言读数,并且后期要循环方便输入多个字符串。
所以我改了下,把gets()函数写进_asm :
#include <stdio.h>
int main()
{
char string[128];
char format[] = "%s";
_asm{
lea eax,string ;
push eax ;
lea eax,format ;
push eax ;
call scanf_s ;
add esp,8 ;
jmp entry
isletter1:
cmp al, 'a'
jl nex
cmp al, 'z'
jg nex
mov ecx, 1
ret
isletter2:
cmp al, 'A'
jl nex
cmp al, 'Z'
jg nex
mov ecx, 1
ret
nex:
mov ecx, 0
ret
entry:
lea edx, string
loopStart:
mov al, byte ptr ds:[edx]
test al, al
jz end
call isletter1
test ecx, ecx
jz test2
sub byte ptr ds:[edx], 'a'-'A'
jmp nextChar
test2:
call isletter2
test ecx, ecx
jz nextChar
add byte ptr ds:[edx], 'a'-'A'
nextChar:
inc edx
jmp loopStart
end:
lea eax,string;
push eax;
call printf;
add esp,4;
}
return 0;
}
为什么这样就不能打印结果了。。该怎么改。。坐等。。跪谢! 汇编 C
[解决办法]
scanf_s还要加一个参以表明最多读取多少位字符.
printf还要有参数format.
以下把LZ程序改为在VS2010中汇编调用scanf_s和printf的写法:
#include <stdio.h>
int main()
{
char string[128];
char format[] = "%s";
_asm{
push 7fH
lea eax,[string] ;
push eax ;
lea eax,[format] ;
push eax ;
call dword ptr scanf_s ;
add esp,8 ;
jmp entry
isletter1:
cmp al, 'a'
jl nex
cmp al, 'z'
jg nex
mov ecx, 1
ret
isletter2:
cmp al, 'A'
jl nex
cmp al, 'Z'
jg nex
mov ecx, 1
ret
nex:
mov ecx, 0
ret
entry:
lea edx, [string]
loopStart:
mov al, byte ptr ds:[edx]
test al, al
jz end
call isletter1
test ecx, ecx
jz test2
sub byte ptr ds:[edx], 'a'-'A'
jmp nextChar
test2:
call isletter2
test ecx, ecx
jz nextChar
add byte ptr ds:[edx], 'a'-'A'
nextChar:
inc edx
jmp loopStart
end:
lea eax,[string] ;
push eax ;
lea eax,[format];
push eax;
call dword ptr printf;
add esp,4;
}
return 0;
}