如何在 Linux 上將資料寫入外部檔案?
例如,如果我開始代碼.include "output.s"并想在其中寫入“1111 0000”,那么如何執行此操作。
uj5u.com熱心網友回復:
進行檔案 I/O 的最佳方式是使用 libc。想象一下你將如何在 C 中做這件事,然后在匯編中做同樣的事情。例如,在 C 中你可以做
int main() {
ssize_t n;
int fd;
char buf[9] = "1111 0000";
fd = creat("output.s", 0666);
if (fd == -1) {
perror("output.s");
return (EXIT_FAILURE);
}
n = write(fd, buf, sizeof buf);
if (n == -1) {
perror("output.s");
return (EXIT_FAILURE);
}
close(fd);
return (EXIT_SUCCESS);
}
接下來,一步一步將代碼翻譯成匯編:
.data
name: .string "output.s" # the file name
buf: .ascii "1111 0000"
buflen= .-buf # the length of buf
.text
.globl main
.type main, @function
main: push %rbx # free rbx and align stack
lea name(%rip), %rdi # file name
mov $0666, %esi # initial permissions
call creat # create the file
cmp $-1,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/519516.html
標籤:Intel Collective linux部件x86-64属性
上一篇:僅使用加法和按位非的二進制函式
