/
sumints_proc.mvm
65 lines (50 loc) · 1.02 KB
/
sumints_proc.mvm
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
main:
; The main procedure has no parameters or locals
enter 0, 0
; Compute the sum of the integers from 1..5
ldc_i 5
call sumints
; print sum
syscall $println
pop
; Note: the ret instruction requires that there is
; at least one operand on the operand stack.
ldc_i 0 ; dummy value
ret
; Procedure to compute the sum of the integers from 1
; to whatever maximum integer is passed as the parameter.
sumints:
; Set up the stack frame:
; one parameter, two local variables
enter 1, 2
; Local variables:
; 0: the value of N
; 1: current sum
; -- you can add other local variables --
ldarg 0 ; this is the value of N
stlocal 0 ; store it in local variable 0
ldc_i 0 ; initial sum value
stlocal 1 ; store it in local variable 1
loop:
; while (N > 0) {
ldlocal 0 ; load N
ldc_i 0 ; constant 0
cmp ; compare them
jlte done
; sum = sum + N
ldlocal 1
ldlocal 0
add
stlocal 1
; N = N - 1
ldlocal 0
ldc_i 1
sub
stlocal 0
; }
jmp loop
done:
; Return the sum
ldlocal 1
ret
; vim:filetype=asm