teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

コードの変更

2021/02/27 15:41

投稿

cha-
cha-

スコア6

title CHANGED
File without changes
body CHANGED
@@ -1,89 +1,54 @@
1
- 下のアセンブリプログラム(x86)を一部変更して入力した数字の**約数**を求めたいです。
1
+ 下のアセンブリプログラム(x86)を入力した数字の約数を求めたいです。
2
2
 
3
- %include "asm_io.inc"
3
+ 下のコードですと、エラーが出てしまいます。
4
4
 
5
5
  segment .data
6
- Message db "Find primes up to: ", 0
6
+ Message db "Find primes up to: ", 0
7
7
 
8
8
 
9
9
  segment .bss
10
- Limit resd 1 ; find primes up to this limit
10
+ Limit resd 1 ; find primes up to this limit
11
- Guess resd 1 ; the current guess for prime
11
+ Guess resd 1 ; the current guess for prime
12
12
 
13
-
14
-
15
13
  segment .text
16
- global asm_main
14
+ global asm_main
17
15
  asm_main:
18
- enter 0,0 ; setup routine
16
+ enter 0,0 ; setup routine
19
- pusha
17
+ pusha
20
18
 
21
- mov eax, Message
19
+ mov eax, Message
22
- call print_string
20
+ call print_string
23
-
21
+
24
- call read_int ; scanf("%u", & limit );
22
+ call read_int ; scanf("%u", & limit );
25
- mov [Limit], eax
23
+ mov [Limit], eax
26
24
 
27
- mov eax, 2 ; printf("2\n");
25
+ mov eax, 2 ; printf("2\n");
28
- call print_int
26
+ call print_int
29
- call print_nl
27
+ call print_nl
30
- mov eax, 3 ; printf("3\n");
28
+ mov eax, 3 ; printf("3\n");
31
- call print_int
29
+ call print_int
32
- call print_nl
30
+ call print_nl
33
31
 
34
- mov dword [Guess], 5 ; Guess = 5;
32
+ mov dword [Guess], 5 ; Guess = 5;
35
33
 
36
- while_limit: ; while ( Guess <= Limit )
37
- mov eax,[Guess]
34
+ loop_start:
38
- cmp eax, [Limit]
35
+ mov eax,[Limit]
36
+     mov ecx,eax
37
+ add eax,ecx
38
+ je eax,0
39
- jnbe end_while_limit ; use jnbe since numbers are unsigned
39
+ movabs ecx,eax
40
+ call print_int
41
+ call print_nl
42
+ loop loop_start
43
+
44
+ leave
45
+ ret
40
46
 
41
- mov ebx, 3 ; ebx is factor = 3;
42
- while_factor:
43
- mov eax,ebx
44
- mul eax ; edx:eax = eax*eax
45
- jo end_while_factor ; if answer won't fit in eax alone
46
- cmp eax, [Guess]
47
- jnb end_while_factor ; if !(factor*factor < guess)
48
- mov eax,[Guess]
49
- mov edx,0
50
- div ebx ; edx = edx:eax % ebx
51
- cmp edx, 0
52
- je end_while_factor ; if !(guess % factor != 0)
53
47
 
54
- add ebx,2 ; factor += 2;
55
- jmp while_factor
56
- end_while_factor:
57
- je end_if ; if !(guess % factor != 0)
58
- mov eax,[Guess] ; printf("%u\n")
59
- call print_int
60
- call print_nl
61
- end_if:
62
- mov eax,[Guess]
63
- add eax, 2
64
- mov [Guess], eax ; guess += 2
65
- jmp while_limit
66
- end_while_limit:
67
48
 
68
- popa
69
- mov eax, 0 ; return back to C
70
- leave
71
-
72
-
73
- ret
74
-
75
- 上のコードは、素数を見つけるプログラムですが、これを約数に見つけるコードにしたいです。
76
-
77
49
  Cでいうと、
78
50
  printf("Recieves a number(limit) and prints its all divisors");
79
51
  for(int i = 1; i <= limit; i++) {
80
52
  if((limit%i) == 0){
81
53
  printf("\n%d", i);
82
-
83
- で、アセンブリで
54
+ だと思うのです。
84
- loop loop start
85
- mov eax
86
- call print_string
87
- call print_nl
88
- jmp end_if
89
- だとは思うのですが、iとlimitの部分をアセンブリでループの中で定義すればいいのかわかりません

2

さらに詳しく

2021/02/27 15:41

投稿

cha-
cha-

スコア6

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- 下のアセンブリプログラムを一部変更して入力した数字の約数を求めたいです。
1
+ 下のアセンブリプログラム(x86)を一部変更して入力した数字の**約数**を求めたいです。
2
2
 
3
3
  %include "asm_io.inc"
4
4
 
@@ -82,7 +82,7 @@
82
82
 
83
83
  で、アセンブリでは
84
84
  loop loop start
85
- mov
85
+ mov eax
86
86
  call print_string
87
87
  call print_nl
88
88
  jmp end_if

1

アセンブリの分からない部分を詳しくした

2021/02/27 03:22

投稿

cha-
cha-

スコア6

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- 下のnasmのアセンブリプログラムを一部変更して入力した数字の約数を求めたいです。
1
+ 下のアセンブリプログラムを一部変更して入力した数字の約数を求めたいです。
2
2
 
3
3
  %include "asm_io.inc"
4
4
 
@@ -72,11 +72,18 @@
72
72
 
73
73
  ret
74
74
 
75
+ 上のコードは、素数を見つけるプログラムですが、これを約数に見つけるコードにしたいです。
75
76
 
76
-
77
77
  Cでいうと、
78
78
  printf("Recieves a number(limit) and prints its all divisors");
79
79
  for(int i = 1; i <= limit; i++) {
80
80
  if((limit%i) == 0){
81
81
  printf("\n%d", i);
82
+
83
+ で、アセンブリでは
84
+ loop loop start
85
+ mov
86
+ call print_string
87
+ call print_nl
88
+ jmp end_if
82
- だとは思うのですが、これnasmアセンブリる方法がわかりません
89
+ だとは思うのですが、iとlimitの部分をアセンブリでループの中で定義ればいいのかわかりません