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?

中古ノート、買ってみた。 その107

0
Posted at

概要

中古ノート買ってみた。
wsl1のubuntu18.04にnasm入れてみた。
nasmをgasに置き換えてみた。
練習問題やってみた。

練習問題

最初nを入力して、次にn個、数字を入力して、ソートして結果を表示せよ。

実行結果

# gcc sort1.s -o sort1 -no-pie

# ./sort1
3
3 2 1
1       2       3       

サンプルコード


.intel_syntax noprefix
.global main
.extern scanf, printf
.text

main:
	push 	rbp
	mov 	rbp, rsp
	sub 	rsp, 128
	lea 	rdi, fmti[rip]
	lea 	rsi, [rsp + 104]
	call 	scanf

	mov 	r13, [rsp + 104]
	mov 	r9, [rsp + 104]
	mov		r14, 0
_input:
	lea 	rdi, fmti[rip]
	lea 	rsi, [rsp + 104]
	call	scanf

	mov 	rax, [rsp + 104]
	lea		rbx, arry[rip]
	add		rbx, r14
	mov		dword [rbx], rax
	add		r14, 4
	dec 	r13
	jnz 	_input

_sort:
	lea		rdi, arry[rip]
	mov		eax, dword ptr [rdi]
	mov		ecx, dword ptr [rdi + 4]
	cmp		eax, ecx
	mov		edx, ecx
	cmovl	edx, eax
	cmovg	ecx, eax
	mov		eax, dword ptr [rdi + 8]
	mov		esi, dword ptr [rdi + 12]
	cmp		eax, esi
	mov		r8d, esi
	cmovl	r8d, eax
	cmovg	esi, eax
	cmp		edx, r8d
	mov		eax, r8d
	cmovl	eax, edx
	cmovg	r8d, edx
	mov		dword ptr [rdi], eax
	cmp		ecx, esi
	mov		eax, esi
	cmovl	eax, ecx
	cmovg	esi, ecx
	mov		dword ptr [rdi + 12], esi
	cmp		eax, r8d
	mov		ecx, r8d
	cmovl	ecx, eax
	cmovle	eax, r8d
	mov		dword ptr [rdi + 8], eax
	mov		dword ptr [rdi + 4], ecx

_print:
	lea		rbx, arry[rip]
	mov		rax, dword [rbx]
	lea 	rdi, fmto[rip]
	mov 	rsi, rax
	call	printf

	add		rbx, 4
	mov		rax, dword [rbx]
	lea 	rdi, fmto[rip]
	mov 	rsi, rax
	call	printf

	add		rbx, 4
	mov		rax, dword [rbx]
	lea 	rdi, fmto[rip]
	mov 	rsi, rax
	call	printf

	xor 	eax, eax
	mov 	rsp, rbp
	pop 	rbp
	ret

.data
	fmti: 	.string "%d"
	fmto: 	.string "%d\t"

.bss
	.comm 	arry, 16


以上。

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?