0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GNU as x86サンプル

Posted at

はじめに

先ずCのアセンブリ出力を参考にし、x64版とx86版を作る。

環境

  • Windows 11 Pro (64bit)
  • WSL (Ubuntu 22.04)

gcc

x86用のコンパイルには追加のインストールが必要。
Ubuntuの場合

$ sudo apt install libc6-dev-i386
fig1.c
// cc -m32 -o fig1 fig1.c
// cc -m32 -masm=intel -S -fno-asynchronous-unwind-tables fig1.c
#include <unistd.h>

int main(void)
{
	write(1, "hello\n", 6);
}

GNU as x64

fig2.asm
# Assembly (as, x64, Linux)
# as -o fig2.o fig2.asm
# ld -o fig2 fig2.o

	.intel_syntax noprefix

	.section .rodata
_msg:	.ascii	"hello, world\n"
	.equ	_len, ($ - _msg)

	.text
	.global _start
_start:
	mov	edx, _len
	lea	rsi, _msg
	mov	edi, 1		# stdout
	mov	eax, 1		# write
	syscall

	xor	edi, edi
	mov	eax, 60		# exit
	syscall

GNU as x86

fig3.asm
# as --32 -o fig3.o fig3.asm
# ld -m elf_i386 -o fig3 fig3.o

	.intel_syntax noprefix

	.section .rodata
_msg:	.ascii	"hello, world\n"
	.equ	_len, ($ - _msg)

	.text
	.global _start
_start:
	mov	edx, _len
	lea	ecx, _msg
	mov	ebx, 1		# stdout
	mov	eax, 4		# write
	int	0x80

	xor	ebx, ebx
	mov	eax, 1		# exit
	int	0x80
$ file fig3
fig3: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?