-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2_tns4616.s
88 lines (73 loc) · 2.45 KB
/
p2_tns4616.s
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
/******************************************************************************
* @file p2_tns4616.s
* @computing number of integer partitions for a positive integer
* n with parts up to m
* @author Tanmay Sardesai, 1001094616
******************************************************************************/
.global main
.func main
main:
BL _prompt
BL _scanf
MOV R4,R0
BL _prompt
BL _scanf
MOV R5,R0
MOV R1,R4
MOV R2,R5
MOV R7,#0
MOV R0,#0
BL _count_partition
ADD R0,R0,R7
MOV R1,R0
MOV R2,R4
MOV R3,R5
BL _print_val
B main
_print_val:
PUSH {LR} @ store LR since printf call overwrites
LDR R0,=result_str @ string at label resultstr:
BL printf @ call printf, where R1 is the print argument
POP {LR}
MOV PC, LR @ return
_prompt:
MOV R7, #4 @ write syscall, 4
MOV R0, #1 @ output stream to monitor, 1
MOV R2, #17 @ print string length
LDR R1, =prompt_str @ string at label prompt_str:
SWI 0 @ execute syscall
MOV PC, LR @ return
_scanf:
PUSH {LR} @ store LR since scanf call overwrites
SUB SP, SP, #4 @ make room on stack
LDR R0, =format_str @ R0 contains address of format string
MOV R1, SP @ move SP to R1 to store entry on stack
BL scanf @ call scanf
LDR R0, [SP] @ load value at SP into R0
ADD SP, SP, #4 @ restore the stack pointer
POP {LR}
MOV PC, LR @ return
_count_partition:
PUSH {LR} @ Push LR onto the stack
CMP R1,#0 @ compare R1 to #0
MOVEQ R0,#1 @ set R0 to #1 if equal
POPEQ {PC} @ POP to PC from stack if equal
MOVLT R0,#0 @ set R0 to #0 if less than
POPLT {PC} @ POP to PC from stack if less than
CMP R2,#0 @ compare R2 to #0
MOVEQ R0,#0 @ set R0 to #0 if equal
POPEQ {PC} @ POP to PC from stack if equal
PUSH {R1} @ PUSH R1 to stack
PUSH {R2} @ PUSH R2 to stack
SUB R1,R1,R2 @ Subtract R2 from R1
BL _count_partition @ Branch with linker to _count_partition (First recurssion)
ADD R7,R7,R0 @ Add return from count to R7
POP {R2} @ POP to register R2
POP {R1} @ POP to register R1
SUB R2,R2,#1 @ Subract 1 from R2
BL _count_partition @ Branch with linker to _count_partition (Second recurssion)
POP {PC} @ Pop to PC from stack
.data
prompt_str: .ascii "Enter a number : "
format_str: .asciz "%d"
result_str: .asciz "There are %d partitions of %d using integers up to %d\n"