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?

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

0
Last updated at Posted at 2025-11-09

概要

中古ノート買ってみた。
wsl1のubuntu18.04にnasm入れてみた。
アセンブラで、atcoder、見つけたので、やってみた。

参考にしたページ

練習問題

ABC 049 C - Daydream
文字列Sが与えられ、始め空の文字列Tの末尾に "dream", "dreamer", "erase", "eraser"を追加する処理を行って、S=Tにできるか判定する問題です。

投入するソース

erasedream

期待値

YES

サンプルコード


global main
extern printf, scanf
section .text

strcmp:
.loop:
	movsx 	eax, BYTE[rdi]
	movsx 	ecx, BYTE[rsi]
	inc 	rdi
	inc 	rsi
	sub 	eax, ecx
	jne 	.end
	test	BYTE[rsi], 255
	jnz 	.loop
.end:
	ret

main:
	push 	rbp
	mov 	rbp, rsp
	sub 	rsp, 131072
	lea 	rdi, [rel fmt]
	lea 	rsi, [rsp]
	call 	scanf wrt ..plt
	lea 	r13, [rsp]
	lea 	rbx, [rsp]
.strlen:
	inc 	r13
	test 	BYTE[r13], 255
	jnz 	.strlen
.check:
	mov 	r12, r13
	cmp 	rbx, r13
	jl 		.match
	lea 	rdi, [rel YES]
	call 	printf wrt ..plt
	xor 	eax, eax
	mov 	rsp, rbp
	pop 	rbp
	ret

.match:
	lea 	rdi, [r12 - 5]
	lea 	r13, [rdi]
	cmp 	rbx, rdi
	jg 		.no
	lea 	rsi, [rel s1]
	call 	strcmp
	test 	eax, eax
	jz 		.check
	lea 	rdi, [r12 - 5]
	lea 	rsi, [rel s3]
	call 	strcmp
	test 	eax, eax
	jz 		.check
	lea 	rdi, [r12 - 6]
	lea 	r13, [rdi]
	cmp 	rbx, rdi
	jg 		.no
	lea 	rsi, [rel s4]
	call 	strcmp
	test 	eax, eax
	jz 		.check
	lea 	rdi, [r12 - 7]
	lea 	r13, [rdi]
	cmp 	rbx, rdi
	jg 		.no
	lea 	rsi, [rel s2]
	call 	strcmp
	test 	eax, eax
	jz 		.check
.no:
	lea 	rdi, [rel NO]
	call 	printf wrt ..plt
	xor 	eax, eax
	mov 	rsp, rbp
	pop 	rbp
	ret

section .data
	fmt db "%s\n", 10, 0;
	YES db "YES", 0;
	NO  db "NO", 0;
	s1  db "dream", 0;
	s2  db "dreamer", 0;
	s3  db "erase", 0;
	s4  db "eraser", 0;





コンパイル、実行結果

# nasm -f elf64 at9.s

# gcc -o at9 at9.o

# ./at9
erasedream
YES

以上。

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?