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?

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

0
Posted at

概要

中古ノート買ってみた。
wsl1のubuntu18.04にnasm入れてみた。
練習問題、やってみた。

練習問題

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

実行結果

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

# ./bubble2
4
4 3 2 1
4  3  2  1  

# ./bubble2
4
1 2 3 4
4  3  2  1  

# ./bubble2
5
1 2 3 4 5
5  4  3  2  1  

# ./bubble2
6
1 2 3 4 5 6
6  5  4  3  2  1  

サンプルコード



.intel_syntax noprefix
.global main
.text

main:
	push	rbp
	mov 	rbp, rsp
_start:
	lea 	rdi, fmti[rip]
	lea 	rsi, ari[rip]
	call	scanf
	mov 	r15, [ari]
	mov 	r13, [ari]
	lea 	r14, ari[rip]
_input:
	lea 	rdi, fmti[rip]
	lea 	rsi, ari[rip]
	call	scanf
	mov 	rax, [ari]
	add 	r14, 4
	mov 	[r14], rax
	dec 	r13
	jnz 	_input

_sort:
	mov 	r14, r15
	dec 	r14
	mov 	r8, 0
_loop4:
	mov 	r13, r14
_loop3:
	mov 	r12, r13
	add 	r12, r8
_address:
	lea 	r11, ari[rip]
	mov 	rax, 0
	mov 	eax, 4
	mul 	r12d
	add 	rax, r11
	mov 	r10, rax
	add 	rax, 4
	mov 	r9, rax
_if:
	mov 	rbx, [r10]
	mov 	rcx, [r9]
	cmp 	rbx, rcx
	jge 	_pass
_swap:
	mov 	[r10], rcx
	mov 	[r9], rbx
_pass:
	dec 	r13
	jnz 	_loop3
	inc 	r8
	dec 	r14
	jnz 	_loop4

_disp:
	mov 	r13, r15
	lea 	r14, ari[rip]
_loop:
	add 	r14, 4
	lea 	rdi, fmts[rip]
	mov 	rsi, [r14]
	call	printf
	dec 	r13
	jnz 	_loop
_end:
	xor 	eax, eax
	mov 	rsp, rbp
	pop 	rbp
	ret

.data
	fmti:	.string "%d"
	fmto:	.string "%d\n"
	fmts:	.string "%d  "
	fmtd:	.string "%d %d\n"

.bss
	.comm	ari, 40



以上。

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?