-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm_Q3
More file actions
88 lines (70 loc) · 1.63 KB
/
Copy pathasm_Q3
File metadata and controls
88 lines (70 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
INCLUDE Irvine32.inc
;Submit by: Omer Idel, ID: 203347794
;This Program get a number from user and return
;his Collatz value
;f(n)= 0 if n=1, 1+f(n/2) if n > 1 is even, 1+f(3n+1) id n > 1 is odd
.data
MyName BYTE "Omer Idel, ID: 203347794", 10, 13, 0
EnterA BYTE "Please enter a number: ", 10, 13, 0
Res BYTE "The collatz value is: ", 10, 13, 0
count WORD 0
.code
my_main PROC
mov edx, offset MyName
call WriteString
mov edx, offset EnterA
call WriteString
call ReadInt
push eax
call Collatz
mov edx, offset Res
call WriteString
call WriteDec
call exitProcess
my_main ENDP
;------------------------------------------------------------
;This is the collatz function
;input: number (2 Bytes)
;output: ax - the collatz value
;------------------------------------------------------------
Collatz PROC
push ebp
mov ebp, esp
push ecx
push edx
xor eax, eax
mov ax, word ptr [ebp + 8]
shr ax, 1 ;check if even or not
jnc EvenNum ;if even
jmp OddNum ;else
jmp EndCollatz
OddNum:
cmp word ptr [ebp + 8], 1 ;checking the stop conditions
je EndCollatz
mov ax, word ptr [ebp + 8]
mov ecx, 3
mul ecx
inc ax
inc Count
push ax
call Collatz
jmp EndCollatz
EvenNum:
cmp word ptr [ebp + 8], 1 ; checking the stop conditions
je EndCollatz
mov ax, word ptr [ebp + 8]
mov ecx, 2
xor edx, edx
div ecx
inc Count
push ax
call Collatz
EndCollatz:
mov ax, count
pop edx
pop ecx
mov esp, ebp
pop ebp
ret 2
Collatz ENDP
END my_main