jam*_*her 7 linux assembly file
我正在尝试在Linux环境中学习汇编 - x86.我能找到的最有用的教程是用NASM编写一个有用的程序.我自己设置的任务很简单:读取文件并将其写入stdout.
这就是我所拥有的:
section .text ; declaring our .text segment
global _start ; telling where program execution should start
_start: ; this is where code starts getting exec'ed
; get the filename in ebx
pop ebx ; argc
pop ebx ; argv[0]
pop ebx ; the first real arg, a filename
; open the file
mov eax, 5 ; open(
mov ecx, 0 ; read-only mode
int 80h ; );
; read the file
mov eax, 3 ; read(
mov ebx, eax ; file_descriptor,
mov ecx, buf ; *buf,
mov edx, bufsize ; *bufsize
int 80h ; );
; write to STDOUT
mov eax, 4 ; write(
mov ebx, 1 ; STDOUT,
; mov ecx, buf ; *buf
int 80h ; );
; exit
mov eax, 1 ; exit(
mov ebx, 0 ; 0
int 80h ; );
Run Code Online (Sandbox Code Playgroud)
这里的一个关键问题是教程从未提及如何创建缓冲区,bufsize
变量或变量.
我该怎么做呢?
(旁白:经过至少一个小时的搜索,我对学习装配的低质量资源感到震惊.当唯一的文件是在网上交易的传闻时,电脑怎么运行?)
哦,这会很有趣.
汇编语言没有变量.这些是更高级别的语言结构.在汇编语言中,如果你想要变量,你可以自己制作变量.上坡.双向.在雪里.
如果你想要一个缓冲区,你将不得不使用堆栈的某个区域作为缓冲区(在调用适当的堆栈帧设置指令之后),或者使用堆上的某个区域.如果你的堆太小,你将不得不做一个SYSCALL指令(另一个INT 80h)来请求操作系统更多(通过sbrk).
另一种方法是了解ELF格式并在适当的部分创建一个全局变量(我认为它是.data).
任何这些方法的最终结果都是您可以使用的内存位置.但是你唯一真实的"变量",就像你已经习惯了现在看似奇妙的C世界一样,是你的寄存器.它们并不是很多.
汇编程序可能会帮助您使用有用的宏.阅读汇编文档; 我不记得他们在我的头顶.
ASM层面的生活很艰难.
小智 5
您必须在 bss 部分声明缓冲区,并在数据中声明 bufsize
section .data
bufsize dw 1024
section .bss
buf resb 1024
Run Code Online (Sandbox Code Playgroud)