街中で見かけて「グッときた人」の思い出

下のアセンブリプログラム(x86)をで入力した数字の約数を求めたいです。

下のコードですと、エラーが出てしまいます。

segment .data
Message db "Find primes up to: ", 0

segment .bss
Limit resd 1 ; find primes up to this limit
Guess resd 1 ; the current guess for prime

segment .text
global asm_main
asm_main:
enter 0,0 ; setup routine
pusha

mov eax, Message
call print_string

call read_int ; scanf("%u", & limit );
mov [Limit], eax

mov eax, 2 ; printf("2\n");
call print_int
call print_nl
mov eax, 3 ; printf("3\n");
call print_int
call print_nl

mov dword [Guess], 5 ; Guess = 5;

loop_start:
mov eax,[Limit]
mov ecx,eax
add eax,ecx
je eax,0
movabs ecx,eax
call print_int
call print_nl
loop loop_start

leave
ret

Cでいうと、
printf("Recieves a number(limit) and prints its all divisors");
for(int i = 1; i <= limit; i++) {
if((limit%i) == 0){
printf("\n%d", i);
だとは思うのです。

A 回答 (1件)

自分でGCCなりのコンパイラ使ってアセンブリ吐かせてみました?



Ubuntuでgccでコンパイルすればこうなるかなぁ。

  .file "chakisann_03.c"
  .text
  .section .rodata
.LC0:
  .string "Find primes up to:"
.LC1:
  .string "%9s%*[^\n]%*c"
.LC2:
  .string "\n%d"
  .text
  .globl main
  .type main, @function
main:
.LFB6:
  .cfi_startproc
  endbr64
  pushq %rbp
  .cfi_def_cfa_offset 16
  .cfi_offset 6, -16
  movq %rsp, %rbp
  .cfi_def_cfa_register 6
  subq $32, %rsp
  movq %fs:40, %rax
  movq %rax, -8(%rbp)
  xorl %eax, %eax
  leaq .LC0(%rip), %rdi
  movl $0, %eax
  call printf@PLT
  leaq -18(%rbp), %rax
  movq %rax, %rsi
  leaq .LC1(%rip), %rdi
  movl $0, %eax
  call __isoc99_scanf@PLT
  leaq -18(%rbp), %rax
  movl $10, %edx
  movl $0, %esi
  movq %rax, %rdi
  call strtol@PLT
  movl %eax, -24(%rbp)
  movl $1, -28(%rbp)
  jmp .L2
.L4:
  movl -24(%rbp), %eax
  movl $0, %edx
  divl -28(%rbp)
  movl %edx, %eax
  testl %eax, %eax
  jne .L3
  movl -28(%rbp), %eax
  movl %eax, %esi
  leaq .LC2(%rip), %rdi
  movl $0, %eax
  call printf@PLT
.L3:
  addl $1, -28(%rbp)
.L2:
  movl -28(%rbp), %eax
  cmpl -24(%rbp), %eax
  jbe .L4
  movl $10, %edi
  call putchar@PLT
  movl $0, %eax
  movq -8(%rbp), %rcx
  xorq %fs:40, %rcx
  je .L6
  call __stack_chk_fail@PLT
.L6:
  leave
  .cfi_def_cfa 7, 8
  ret
  .cfi_endproc
.LFE6:
  .size main, .-main
  .ident "GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0"
  .section .note.GNU-stack,"",@progbits
  .section .note.gnu.property,"a"
  .align 8
  .long 1f - 0f
  .long 4f - 1f
  .long 5
0:
  .string "GNU"
1:
  .align 8
  .long 0xc0000002
  .long 3f - 2f
2:
  .long 0x3
3:
  .align 8
4:

いずれにせよ、「エラーが出ます」と言うより「どんなエラーなのか」明示した方が良いたぁ思いますけどね。
    • good
    • 0
この回答へのお礼

ありがとうございます♪

お礼日時:2021/03/04 06:03

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!