bits 64 section .note.GNU-stack section .data ; msg db "Hello, world!", 10 ; len equ $ - msg section .text ;char read_char(void) read_char: xor rax, rax xor rdi, rdi lea rsi, [rsp-8] mov rdx, 1 syscall cmp rax, 1 jne read_char_error mov al, [rsp-8] ret read_char_error: mov rax, -1 ret ;int read_int(void) read_int: push rbp mov rbp, rsp sub rsp, 16 mov [rbp-8], rbx ;RBXを一時的な数値の置き場として使うので元の値を覚えておく xor ebx, ebx read_int_loop: call read_char cmp eax, '0' jl read_int_end cmp eax, '9' jg read_int_end sub eax, '0' imul ebx, 10 add ebx, eax jmp read_int_loop read_int_end: mov eax, ebx mov rbx, [rbp-8] leave ret ;void print_char(char) ;RDIにcharが入っている print_char: mov rax, 1 mov [rsp-8], edi mov rdi, 1 lea rsi, [rsp-8] mov rdx, 1 syscall cmp rax, 1 jne print_char_error ret print_char_error: mov rax, -1 ret ;void println(void) println: mov edi, `\n` call print_char ret ;void print_int(int) print_int: mov eax, edi mov r8, rsp mov esi, 10 print_int_loop: cdq idiv esi ;商:EAX,余り:EDX dec rsp mov [rsp], dl add [rsp], byte '0' cmp eax, 0 jne print_int_loop ;出力 mov rax, 1 mov rdi, 1 mov rsi, rsp mov rdx, r8 sub rdx, rsp syscall mov rsp, r8 ret global _start _start : push rbp mov rbp, rsp sub rsp, 32 %define ans [rbp-8] %define l [rbp-16] %define r [rbp-24] call read_int mov l, eax call read_int mov r, eax mov eax, 0 mov ans, eax mov eax, r cmp eax, 296 jl skip mov eax, l cmp eax, 295 jg skip mov eax, ans add eax, 1 mov ans, eax skip: mov eax, r cmp eax, 417 jl skip2 mov eax, l cmp eax, 416 jg skip2 mov eax, ans add eax, 1 mov ans, eax skip2: mov edi, ans call print_int call println ;終了 mov rax, 60 xor rdi, rdi syscall