1
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?

More than 3 years have passed since last update.

アセンブラAdvent Calendar 2018

Day 25

paizaでアセンブラ その5

Last updated at Posted at 2018-12-06

#概要

paizaでアセンブラ、やってみた。
gas調べたら、printf使えた。
fizzbuzzやってみた。

#サンプルコード

import subprocess
import os

def gcc():
    c_file = 'fizzbuzz.s'
    test_c = """
.code64
.text
.global main

main:
	sub 	$8, %rsp
	mov 	$1, %r8
loop:
	mov 	$0, %r10
	mov 	$0, %rdx
	mov 	%r8, %rax
	mov 	$3, %rsi
	div 	%esi
	cmp 	$0, %rdx
	jne 	skip1
	add 	$1, %r10
skip1:
	mov 	$0, %rdx
	mov 	%r8, %rax
	mov 	$5, %rsi
	div 	%esi
	cmp 	$0, %rdx
	jne 	skip2
	add 	$2, %r10
skip2:
	cmp 	$0, %r10
	je 		printNum
	cmp 	$2, %r10
	jb 		printFizz
	je 		printBuzz
	ja 		printFizzBuzz
printNum:
	lea 	num(%rip), %rdi
	jmp 	loope
printFizz:
	lea 	fizz(%rip), %rdi
	jmp 	loope
printBuzz:
	lea 	buzz(%rip), %rdi
	jmp 	loope
printFizzBuzz:
	lea 	fizzbuzz(%rip), %rdi
loope:
	mov 	$1, %rax
	mov 	%r8, %rsi
	push 	%r8
	push 	%r10
	call 	printf
	pop 	%r10
	pop 	%r8
	add 	$1, %r8
	cmp 	$101, %r8
	jne 	loop
	add 	$8, %rsp
	ret
.data
num:
	.asciz "%i "
fizz:
	.asciz "Fizz "
buzz:
	.asciz "Buzz "
fizzbuzz:
	.asciz "FizzBuzz "	
"""
    with open(c_file, 'w') as f:
        f.write(test_c)
    os.system("gcc -no-pie %s" % c_file)
    result = subprocess.Popen('./a.out', stdout = subprocess.PIPE).communicate()[0]
    for i, v in enumerate(result[ : -1].split('\n')):
        print v

if __name__ == '__main__':
  gcc()
  

#成果物

以上。

1
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
1
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?