当前位置: 首页 > news >正文

nasm - console 32bits

文章目录

    • nasm - console 32bits
    • 概述
    • 笔记
    • my_build.bat
    • nasm_main.asm
    • 用VS2019写个程序,按照win32方式编译,比较一下。
    • 备注
    • END

nasm - console 32bits

概述

看到一个nasm的例子(用nasm实现一个32bits控制台的程序架子)
学习一下

笔记

my_build.bat

@echo off
rem my_build.batrem env
rem NASM version 2.16.03 compiled on Apr 17 2024
rem GoLink.Exe Version 1.0.4.6  Copyright Jeremy Gordon 2002-2025cls
set path=C:\Program Files\NASM;D:\my_dev\my_study_re\src\nasm\NasmX86AndX64ProgrammingExamples\tools\Golink;%path%rem .bat默认是不支持中文的
rem echo full path name - %~f0
rem echo full path      - %~dp0
rem echo file name      - %~nx0
rem echo work path      - %cd%if "%1" == "build" (goto build
) else if "%1" == "clear" (goto clear
) else (goto usage
):usage
echo usage my_build.bat [option]
echo build - build asm to EXE
echo clear - clear trush on the project
goto end:build
echo build ...rem find file on work path
if exist "nasm_main.obj"  (del "nasm_main.obj" 
) nasm -f win32 nasm_main.asm -o nasm_main.obj
rem 用IDA打开.obj 已经可以看到实现逻辑了if exist "console_win32.exe"  (del "console_win32.exe" 
) rem 如果不指定要连接的dll, 会报错
golink /entry:Start /console kernel32.dll nasm_main.obj /fo console_win32.exeif exist "console_win32.exe"  (echo run console_win32.exe console_win32.exe 
)
goto end:clear
echo clear ...
if exist "nasm_main.obj"  (del "nasm_main.obj" 
) if exist "console_win32.exe"  (del "console_win32.exe" 
)
goto end:end
echo END
rem pause
call cmd

nasm_main.asm

; @file nasm_main.asm
; @brief 用NASM实现一个32bits控制台程序NULL EQU 0
STD_OUTPUT_HANDLE EQU -11; 调用的win32API不用特意修饰, 用API的原始名称就行, 不必搞成 _WriteFile@20
extern GetStdHandle
extern WriteFile
extern ExitProcessglobal Startsection .dataMessage db "Console Message 32", 0x0D, 0x0AMessageLength EQU $-Messagesection .bssStandardHandle resd 1Written resd 1section .text
Start:push STD_OUTPUT_HANDLEcall GetStdHandlemov dword[StandardHandle], EAXpush NULLpush Writtenpush MessageLengthpush Messagepush dword[StandardHandle]call WriteFilepush NULL
call ExitProcess

用VS2019写个程序,按照win32方式编译,比较一下。


#include <Windows.h>int main()
{const char* pMsg = "Console Message 64 ...";DWORD NumberOfBytesWritten = 0;system("pause");HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);WriteFile(hStdOut, pMsg, strlen(pMsg), &NumberOfBytesWritten, NULL);ExitProcess(0);
}

然后单步调试,断住后,转到反汇编。比较了一下手写的NASM代码,发现基本和反汇编的结果一致。
NASM的语法和VS2019反汇编出来的代码几乎一模一样。

; @file nasm_main.asm
; @brief 用NASM实现一个32bits控制台程序NULL EQU 0
STD_OUTPUT_HANDLE EQU -11; 调用的win32API不用特意修饰, 用API的原始名称就行, 不必搞成 _WriteFile@20
extern GetStdHandle
extern WriteFile
extern ExitProcessglobal Startsection .dataMessage db "Console Message 32", 0x0D, 0x0AMessageLength EQU $-Messagesection .bssStandardHandle resd 1Written resd 1section .text
Start:; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);; 00DB17D4  mov         esi,esp  ; 00DB17D6  push        0FFFFFFF5h // !; 00DB17D8  call        dword ptr [__imp__GetStdHandle@4 (0DBB000h)] // !  ; 00DB17DE  cmp         esi,esp  ; 00DB17E0  call        __RTC_CheckEsp (0DB1235h)  ; 00DB17E5  mov         dword ptr [hStdOut],eax // !push STD_OUTPUT_HANDLEcall GetStdHandlemov dword[StandardHandle], EAX; WriteFile(hStdOut, pMsg, strlen(pMsg), &NumberOfBytesWritten, NULL);; 00DB17E8  mov         esi,esp  ; 00DB17EA  push        0 // !; 00DB17EC  lea         eax,[NumberOfBytesWritten]  ; 00DB17EF  push        eax // !; 00DB17F0  mov         ecx,dword ptr [pMsg]  ; 00DB17F3  push        ecx // !; 00DB17F4  call        _strlen (0DB1366h)  ; 00DB17F9  add         esp,4  ; 00DB17FC  push        eax // ! ; 00DB17FD  mov         edx,dword ptr [pMsg]  ; 00DB1800  push        edx // ! ; 00DB1801  mov         eax,dword ptr [hStdOut]  ; 00DB1804  push        eax // ! ; 00DB1805  call        dword ptr [__imp__WriteFile@20 (0DBB004h)] // !; 00DB180B  cmp         esi,esp  ; 00DB180D  call        __RTC_CheckEsp (0DB1235h)  push NULLpush Writtenpush MessageLengthpush Messagepush dword[StandardHandle]call WriteFile; ExitProcess(0);; 00DB1812  mov         esi,esp  ; 00DB1814  push        0 // !; 00DB1816  call        dword ptr [__imp__ExitProcess@4 (0DBB008h)] // !; 00DB181C  cmp         esi,esp  ; 00DB181E  call        __RTC_CheckEsp (0DB1235h)  push NULLcall ExitProcess

备注

从VS2019 c++ console工程的代码和NASM例子代码比较,可以得到一个提示。
如果用NASM手写代码不太熟练,可以从VS2019 c++代码的反汇编代码抽取汇编代码,直接就能用在NASM工程上。

END

http://www.lryc.cn/news/539557.html

相关文章:

  • 11.编写前端内容|vscode链接Linux|html|css|js(C++)
  • 【deepseek-r1模型】linux部署deepseek
  • 【Github每日推荐】-- 2024 年项目汇总
  • C++中的.*运算符
  • 深度学习笔记——LSTM
  • spring boot知识点2
  • 【机器学习】CNN与Transformer的表面区别与本质区别
  • 框架篇 - Hearth ArcGIS 框架扩展(DryIoC、Options、Nlog...)
  • JUC并发—7.AQS源码分析三
  • windows系统本地部署DeepSeek-R1全流程指南:Ollama+Docker+OpenWebUI
  • 当C#邂逅Deepseek, 或.net界面集成deepseek
  • Cursor实战:Web版背单词应用开发演示
  • Kotlin Lambda
  • V4L2驱动之UVC
  • numpy(01 入门)
  • Chatgpt论文润色指令整理
  • vscode复制到下一行
  • Python天梯赛刷题-五分题(上)
  • 【优先级队列】任务分配
  • 设计模式之适配模式是什么?以及在Spring AOP中的拦截器链的使用源码解析。
  • Python 库自制 Cross-correlation 算法
  • C++(23):为类成员函数增加this参数
  • javaSE学习笔记23-线程(thread)-总结
  • 【DeepSeek服务器部署全攻略】Linux服务器部署DeepSeek R1模型、实现API调用、搭建Web页面以及专属知识库
  • 【JAVA工程师从0开始学AI】,第四步:闭包与高阶函数——用Python的“魔法函数“重构Java思维
  • 算法日记20:SC72最小生成树(prim朴素算法)
  • 玩转SpringCloud Stream
  • 嵌入式经常用到串口,如何判断串口数据接收完成?
  • iOS App的启动与优化
  • 导出指定文件夹下的文件结构 工具模块-Python