-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.ys
49 lines (49 loc) · 1.17 KB
/
sample.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
# 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
#/* $begin sum-ys 0 */
# int Sum(int *Start, int Count)
Sum: pushl %ebp
rrmovl %esp,%ebp
irmovl 8, %edi
addl %edi, %ebp
mrmovl (%ebp),%ecx # ecx = Start
irmovl 12, %edi
addl %edi, %ebp
mrmovl (%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
subl %ebx,%edx # Count--
jne Loop # Stop when 0
End: rrmovl %ebp,%esp
popl %ebp
ret
#/* $end sum-ys 0 */
# The stack starts here and grows to lower addresses
.pos 0x100
Stack: