-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample47.ys
53 lines (49 loc) · 1.22 KB
/
sample47.ys
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
# Execution begins at address 0
.pos 0
init:
irmovl Stack, %esp # set up stack pointer
irmovl Stack, %ebp # set up base pointer
call Main # execute main program
halt # terminate program
# array of 4 elements
.align 4
array:
.long 0xd
.long 0xc0
.long 0xb00
.long 0xa000
Main:
pushl %ebp
rrmovl %esp, %ebp
irmovl $4, %eax
pushl %eax # push 4
irmovl array, %edx
pushl %edx # push array
call Sum # Sum(array, 4)
rrmovl %ebp, %esp
popl %ebp
ret
# int Sum(int *Start, int Count)
Sum:
pushl %ebp
rrmovl %esp, %ebp
mrmovl 8(%ebp), %ecx # %ecx = Start
mrmovl 12(%ebp), %edx # %edx = Count
xorl %eax, %eax # sum = 0
andl %edx, %edx # set condition codes
je End
Loop:
mrmovl (%ecx), %esi # get *Start
addl %esi, %eax # add to sum
irmovl $4, %ebx #
addl %ebx, %ecx # Start++
irmovl $-1, %ebx #
addl %ebx, %edx # Count--
jne Loop # stop when 0
End:
rrmovl %ebp,%esp
popl %ebp
ret
# The stack starts here and grows
.pos 0x100
Stack: