小编Sec*_*Sam的帖子

如何将C++子例程链接到x86汇编程序?

我正在尝试制作一个简单的汇编程序,打印出"Hello!" 一次,等待一秒钟,然后再打印出来.由于睡眠功能在组装时相对复杂,而且我不擅长它,所以我决定使用C++来制作Sleep子程序.这是C++程序:

// Sleep.cpp
#include <thread>
#include <chrono>

void Sleep(int TimeMs) {
    std::this_thread::sleep_for(std::chrono::milliseconds(TimeMs));
}
Run Code Online (Sandbox Code Playgroud)

然后我使用"gcc -S Sleep.cpp"将这个睡眠函数编译成汇编程序,然后使用"gcc -c Sleep.s"将其编译成目标文件.

我试图从汇编中调用这个C++子例程.我听说你通过将它们推入堆栈来为C++子程序提供参数,这是我到目前为止的汇编代码:

        global    _main
        extern    _puts
        extern    Sleep
        section   .text
_main:    
        push    rbp
        mov     rbp,    rsp
        sub     rsp,    32


        ;Prompt user:
        lea     rdi,    [rel prompt]        ; First argument is address of message
        call    _puts                       ; puts(message)

        push    1000 ; Wait 1 second (Sleep time is in milliseconds)
        call    Sleep

        lea     rdi,    [rel prompt] ; Print hello again
        call    _puts

        xor     rax,    rax …
Run Code Online (Sandbox Code Playgroud)

c++ x86 assembly gcc nasm

5
推荐指数
1
解决办法
417
查看次数

在x86汇编中打印它们时,ANSI转义码无法正常工作?

我正在尝试制作一个简单的x86汇编程序(我使用NASM作为我的汇编程序),它使用ANSI代码将终端文本颜色更改为红色,然后打印一些将用红色前景打印的内容.代码如下:

; This macro is equivalent to printf(message)
%macro  print   1
        lea     rdi,    [rel %1]
        call    _printf
%endmacro
; Example call:
;   print    prompt
; Where prompt is something like:
;   prompt:
;       db      "Hiya dude! What's your name?", 0

; These are the terminal colors, they are ANSI codes that, when printed, will change the color of the text.
    section .data
COLOR_FORE_RED:
    db       "\033[31m",0 ; ANSI Fore Red code

%define  SetColor_FRed      print    COLOR_FORE_RED
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用以下内容时使用这些宏:

SetColorFRed ; Set text color …
Run Code Online (Sandbox Code Playgroud)

io x86 assembly printf nasm

4
推荐指数
1
解决办法
200
查看次数

标签 统计

assembly ×2

nasm ×2

x86 ×2

c++ ×1

gcc ×1

io ×1

printf ×1