鱼C论坛

 找回密码
 立即注册
查看: 1322|回复: 2

[已解决]乱码

[复制链接]
发表于 2023-10-9 20:47:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. ; 使用 Win32ASM 写的 Hello, world 程序
  2. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  3. ; 使用 nmake 或下列命令进行编译和链接:
  4. ; ml /c /coff Hello.asm
  5. ; Link /subsystem:windows Hello.obj
  6. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  7.                 .386
  8.                 .model flat,stdcall
  9.                 option casemap:none
  10. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  11. ; Include 文件定义
  12. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  13. include                \masm32\include\windows.inc
  14. include                \masm32\include\user32.inc
  15. includelib        \masm32\lib\user32.lib
  16. include                \masm32\include\kernel32.inc
  17. includelib        \masm32\lib\kernel32.lib
  18. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  19. ; 数据段
  20. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  21.                 .data
  22. szCaption        db        '来自鱼C工作室', 0
  23. szText                db        'Hello, World !', 0

  24. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  25. ; 代码段
  26. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  27.                 .code
  28. start:
  29.                 invoke        MessageBox,NULL,offset szText,offset szCaption,MB_OK
  30.                 invoke        ExitProcess,NULL
  31. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  32.                 end        start





复制代码





如图:编译链接之后,执行时中文是乱码  ?


最佳答案
2023-10-9 20:48:15
这是因为在代码中使用了中文字符,而编译器默认使用的字符集与系统默认字符集不一致导致的。解决方法是在代码开头添加以下语句:

  1. .586
  2. .model flat, stdcall
  3. option casemap:none
  4. include \masm32\include\windows.inc
  5. include \masm32\include\user32.inc
  6. includelib \masm32\lib\user32.lib
  7. include \masm32\include\kernel32.inc
  8. includelib \masm32\lib\kernel32.lib
  9. .code
  10. start:
  11.     sub esp, 8
  12.     push 0
  13.     push offset szCaption
  14.     push offset szText
  15.     push 0
  16.     call MessageBoxW
  17.     add esp, 16
  18.     push 0
  19.     call ExitProcess
  20. end start
复制代码


在代码中使用  MessageBoxW  而不是  MessageBox ,并将  szCaption  和  szText  的类型从  db  改为  dw ,同时在字符串前添加  L  来表示它们是 Unicode 字符串。这样就可以正确地显示中文字符了。
1111.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-9 20:48:15 | 显示全部楼层    本楼为最佳答案   
这是因为在代码中使用了中文字符,而编译器默认使用的字符集与系统默认字符集不一致导致的。解决方法是在代码开头添加以下语句:

  1. .586
  2. .model flat, stdcall
  3. option casemap:none
  4. include \masm32\include\windows.inc
  5. include \masm32\include\user32.inc
  6. includelib \masm32\lib\user32.lib
  7. include \masm32\include\kernel32.inc
  8. includelib \masm32\lib\kernel32.lib
  9. .code
  10. start:
  11.     sub esp, 8
  12.     push 0
  13.     push offset szCaption
  14.     push offset szText
  15.     push 0
  16.     call MessageBoxW
  17.     add esp, 16
  18.     push 0
  19.     call ExitProcess
  20. end start
复制代码


在代码中使用  MessageBoxW  而不是  MessageBox ,并将  szCaption  和  szText  的类型从  db  改为  dw ,同时在字符串前添加  L  来表示它们是 Unicode 字符串。这样就可以正确地显示中文字符了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-9 20:53:06 | 显示全部楼层
用记事本打开,另存为ANSI编码就行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 12:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表