FASM 将 Hello World 写入控制台,完全没有包含或依赖项 [英] FASM write Hello World to console with NO includes or dependencies at all

查看:100
本文介绍了FASM 将 Hello World 写入控制台,完全没有包含或依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过

如何在Windows下用汇编程序编写hello world?

使用 DOS 在 Fasm 中编写 hello,world 到控制台

如何在 fasm 中写入控制台?

我已经尝试/看到过这样的代码,例如来自 这个答案

I've tried / seen code like this MASM example from this answer

;---ASM Hello World Win64 MessageBox

extrn MessageBoxA: PROC
extrn ExitProcess: PROC

.data
title db 'Win64', 0
msg db 'Hello World!', 0

.code
main proc
  sub rsp, 28h  
  mov rcx, 0       ; hWnd = HWND_DESKTOP
  lea rdx, msg     ; LPCSTR lpText
  lea r8,  title   ; LPCSTR lpCaption
  mov r9d, 0       ; uType = MB_OK
  call MessageBoxA
  add rsp, 28h  
  mov ecx, eax     ; uExitCode = MessageBox(...)
  call ExitProcess
main endp

End

(我在 Windows 64 位 extrn MessageBoxA:PROC 上收到错误非法指令",因为 FASM 不理解该 MASM 指令.)

(to which I get an error "Illegal instruction" on windows 64 bit extrn MessageBoxA:PROC because FASM doesn't understand that MASM directive.)

还有这个 FASM 示例 来自这个问题

also this FASM example from this question

 ; Example of 64-bit PE program


format PE64 GUI 
entry start 

section '.text' code readable executable 

  start: 
      sub     rsp,8*5         ; reserve stack for API use and make stack dqword aligned 

    mov     r9d,0 
    lea     r8,[_caption] 
    lea     rdx,[_message] 
    mov    rcx,0 
    call    [MessageBoxA] 

    mov     ecx,eax 
    call    [ExitProcess] 

section '.data' data readable writeable 

  _caption db 'Win64 assembly program',0 
  _message db 'Hello World!',0 

section '.idata' import data readable writeable 

  dd 0,0,0,RVA kernel_name,RVA kernel_table 
  dd 0,0,0,RVA user_name,RVA user_table 
  dd 0,0,0,0,0 

  kernel_table: 
    ExitProcess dq RVA _ExitProcess 
    dq 0 
  user_table: 
    MessageBoxA dq RVA _MessageBoxA 
    dq 0 

  kernel_name db 'KERNEL32.DLL',0 
  user_name db 'USER32.DLL',0 

  _ExitProcess dw 0 
    db 'ExitProcess',0 
  _MessageBoxA dw 0 
    db 'MessageBoxA',0

但它显示一个消息框并且还具有外部依赖项kernel32.dll"和user32.dll"

but it displays a message box and also has external dependencies "kernel32.dll" and "user32.dll"

还尝试了这个例子 来自 FASM 论坛

format pe console


include 'win32ax.inc'

entry main

section '.data!!!' data readable writeable

strHello db 'Hello World !',13,10,0
strPause db 'pause',0

section '.txt' code executable readable

main:
       ; you can use crt functions or windows API.
       cinvoke printf,strHello
       cinvoke system,strPause; or import getc()
       ; or
       ; invoke printf,srtHello
       ; add esp, 4

       ; or use WriteFile and GetStdHandle APIs
       push 0
       call [ExitProcess]
      
section '.blah' import data readable

library kernel32,'kernel32.dll',\
    msvcrt,'msvcrt.dll'    ;; C-Run time from MS. This is always on every windows machine

import kernel32,\
          ExitProcess,'ExitProcess'
import msvcrt,\
          printf,'printf',\
          system,'system'

但它依赖于 win32ax.inc 和其他导入

but it depends on win32ax.inc and other imports

还有

format PE console
include 'win32ax.inc'
.code
start:
        invoke  WriteConsole,<invoke GetStdHandle,STD_OUTPUT_HANDLE>,"Hello World !",13,0
        invoke  Sleep,-1
.end start

但需要win32ax.inc";导入

but requires "win32ax.inc" import

在没有 win32ax 的情况下我能找到的最接近的来自 FASM 论坛:

closest I could find without the win32ax from the FASM forum:

format pe64 console
entry start

STD_OUTPUT_HANDLE       = -11

section '.text' code readable executable

start:
        sub     rsp,8*7         ; reserve stack for API use and make stack dqword aligned
        mov     rcx,STD_OUTPUT_HANDLE
        call    [GetStdHandle]
        mov     rcx,rax
        lea     rdx,[message]
        mov     r8d,message_length
        lea     r9,[rsp+4*8]
        mov     qword[rsp+4*8],0
        call    [WriteFile]
        mov     ecx,eax
        call    [ExitProcess]

section '.data' data readable writeable

message         db 'Hello World!',0
message_length  = $ - message

section '.idata' import data readable writeable

        dd      0,0,0,RVA kernel_name,RVA kernel_table
        dd      0,0,0,0,0

kernel_table:
        ExitProcess     dq RVA _ExitProcess
        GetStdHandle    dq RVA _GetStdHandle
        WriteFile       dq RVA _WriteFile
                        dq 0

kernel_name     db 'KERNEL32.DLL',0
user_name       db 'USER32.DLL',0

_ExitProcess    db 0,0,'ExitProcess',0
_GetStdHandle   db 0,0,'GetStdHandle',0
_WriteFile      db 0,0,'WriteFile',0    

但仍需要 kernel32.dll 和 user32.dll

but still requires the kernel32.dll and user32.dll

有什么方法可以在没有任何外部 DLL 的情况下做到这一点?我知道只有程序 fasm 自己做,然后打印到控制台,不是吗?

Any way to do this without any external DLLs at all? I know just the program fasm itself does it, and prints to the console, doesn't it?

推荐答案

没有任何外部 DLL 的任何方法可以做到这一点?

Any way to do this without any external DLLs at all?

在 Windows 下:绝对没有!

Under Windows: Definitely no!

Windows 使用一些方法(可能是 syscall)进入操作系统,但是没有官方入口点.

Windows uses some methods (probably syscall) to enter the operating system, however, there are no official entry points.

这意味着在当前 Windows 版本中显示 Hello world" 消息框的完全相同的程序有可能(不太可能但是)在下一次 Windows 更新后执行完全不同的操作!

This means that it is (unlikely but) possible that exactly the same program that shows the "Hello world" message box in the current Windows version will do something completely different after the next Windows update!

因为微软假设每个 Windows 程序只使用与内核版本匹配的 .dll 文件调用操作系统,所以他们可以做到这一点.

Because Microsoft is assuming that every Windows program is only calling the OS by using the .dll files that match the kernel version, they can do this.

我不了解 Windows 10,但较旧的 Windows 版本(我不记得是 XP、Vista 还是 7)甚至只是假设 .exe 文件会立即返回如果它不使用任何 .dll 文件:在这种情况下程序甚至没有启动!

I don't know about Windows 10, but an older Windows version (I don't remember if it was XP, Vista or 7) even simply assumed that an .exe file returns at once if it does not use any .dll file: The program was not even started in this case!

这篇关于FASM 将 Hello World 写入控制台,完全没有包含或依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆