这是我现在正在玩的代码:
# file-name: test.s
# 64-bit GNU as source code.
.global main
.section .text
main:
lea message, %rdi
push %rdi
call puts
lea message, %rdi
push %rdi
call printf
push $0
call _exit
.section .data
message: .asciz "Hello, World!"
Run Code Online (Sandbox Code Playgroud)
编译说明:gcc test.s -o test
修订版 1:
.global main
.section .text
main:
lea message, %rdi
call puts
lea message, %rdi
call printf
mov $0, %rdi
call _exit
.section .data
message: .asciz "Hello, World!"
Run Code Online (Sandbox Code Playgroud)
最终修订版(作品):
.global main
.section .text
main:
lea message, %rdi …Run Code Online (Sandbox Code Playgroud) 幸运的是,学习计算机编程艺术的所有用途都可以访问Stack Overflow等社区!我决定承担学习如何编程计算机的任务,我正在通过一本名为"从头开始编程"的电子书的知识这样做,该电子书教会读者如何用汇编语言创建程序在GNU/Linux环境中.
我在本书中的进展已经到了创建一个程序,该程序用函数计算整数4的阶乘,我已经完成并完成了没有由GCC的汇编程序引起的或由运行程序引起的任何错误.但是,我的程序中的功能没有返回正确的答案!阶乘4是24,但程序返回值0!说对了,我不知道为什么会这样!
以下是供您考虑的代码:
.section .data
.section .text
.globl _start
.globl factorial
_start:
push $4 #this is the function argument
call factorial #the function is called
add $4, %rsp #the stack is restored to its original
#state before the function was called
mov %rax, %rbx #this instruction will move the result
#computed by the function into the rbx
#register and will serve as the return
#value
mov $1, %rax #1 must be placed inside this register for
#the exit system …Run Code Online (Sandbox Code Playgroud) 我知道这int 0x80会在 linux 中造成中断。但是,我不明白这段代码是如何工作的。它会返回一些东西吗?
代表什么$ - msg?
global _start
section .data
msg db "Hello, world!", 0x0a
len equ $ - msg
section .text
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, len
int 0x80 ;What is this?
mov eax, 1
mov ebx, 0
int 0x80 ;and what is this?
Run Code Online (Sandbox Code Playgroud) 我编写了一个代码,在C++中什么都不做
void main(void){
}
Run Code Online (Sandbox Code Playgroud)
和大会.
.global _start
.text
_start:
mov $60, %rax
xor %rdi, %rdi
syscall
Run Code Online (Sandbox Code Playgroud)
我编译C代码并编译和链接汇编代码.我用time命令比较了两个可执行文件.
部件
time ./Assembly
real 0m0.001s
user 0m0.000s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
C
time ./C
real 0m0.002s
user 0m0.000s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
汇编速度比C快两倍.我反汇编代码,在汇编代码中,只有四行代码(相同).在C代码中,有大量不必要的代码用于将main链接到_start.主要有四行代码,其中三行代表不可能(你不能从函数博客外部访问函数的变量)从' block ' 外部访问' local '(如函数变量)变量(像功能块一样).
push %rbp ; push base pointer.
mov %rsp, %rbp ; copy value of stack pointer to base pointer, stack pointer is using for saving variables.
pop %rbp ; 'local' variables are removed, because we pop the base …Run Code Online (Sandbox Code Playgroud)