内联汇编跪求解答

yyyegr 2013-11-23 11:13:34
输入字符串,转化大小写。

原代码是这样的:
#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;
}

为什么这样就不能打印结果了。。该怎么改。。坐等。。跪谢!
...全文
126 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2013-11-24
  • 打赏
  • 举报
回复
变量名也可以不加括号: lea eax,string
logiciel 2013-11-24
  • 打赏
  • 举报
回复
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;
}
yyyegr 2013-11-24
  • 打赏
  • 举报
回复
嗯。。是最后的esp的问题。。我好像弄好了。。谢谢啦。
yyyegr 2013-11-24
  • 打赏
  • 举报
回复
logiciel...为什么按你的写还是会报错。。 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention. 麻烦你再看一下。。谢啦。。
yyyegr 2013-11-23
  • 打赏
  • 举报
回复
顶。。。。。。求解答。。

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧