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?

gccとアセンブラの連携

Posted at

Cからシステムコール

fig1.c
// C (gcc)
// cc -masm=intel -o fig1 fig1.c

void hoge(char *str, int len);
__asm__("\n\
hoge:						\n\
	mov	edx, esi	# len		\n\
	mov	rsi, rdi	# str		\n\
	mov	edi, 1		# stdout	\n\
	mov	eax, 1		# write		\n\
	syscall					\n\
	ret					\n\
");

int main(void)
{
	char msg[] = "hello, world\n";
	hoge(msg, sizeof msg - 1);
}

アセンブラからprintf

fig2.s
// C (gcc) -xassembler
// cc -o fig2 fig2.s

	.intel_syntax noprefix
	.section	.rodata
fmt:	.string	"hello, %d\n"

	.text
	.globl	main
main:
	push	rbp
	mov	rbp, rsp

	mov	esi, 42
	lea	rdi, fmt[rip]
	call	printf

	pop	rbp
	ret

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?