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
89
90
|
#------------ t.s file generated by 64-bit GCC compiler ------------–
.globl sub
sub: # int sub(int a,b,c,d,e,f,g,h)
# first 6 parameters | a |,| b |,| c |,| d |,| e |,| f |
# in registers: |rdi|,|rsi|,|rdx|,|rcx|,|r8d|,|r9d|
# 2 extra parameters g,h are on stack.
# Upon entry, stack top contains g, h
# -------------------------------------
# . . . ...| h | g | PC | LOW address
^
# RSP
# ------------------------------------
# establish stack frame
pushq %rbp
movq %rsp, %rbp
# no need to shift rsp down because each function has a 128 bytes
# reserved stack area.
# rsp will be shifted down if function define more locals
# save first 6 parameters in registers on stack
movl %edi, -20(%rbp) # a
movl %esi, -24(%rbp) # b
movl %edx, -28(%rbp) # C
movl %ecx, -32(%rbp) # d
movl %r8d, -36(%rbp) # e
movl %r9d, -40(%rbp) # f
# access locals u, v, w at rbp -4 to -12
movl $9, -4(%rbp)
movl $10, -8(%rbp)
movl $11, -12(%rbp)
# compute x + g + u + v:
movl -20(%rbp), %edx # saved a on stack
movl 16(%rbp), %eax # g at 16(rbp)
addl %eax, %edx
movl -4(%rbp), %eax # u at -4(rbp)
addl %eax, %edx
movl -8(%rbp), %eax # v at -8(rbp)
addl %edx, %eax
# did not shift rsp down, so just popQ to restore rbp
popq %rbp
ret
# ====== main function code in assembly ======
.globl main
main:
# establish stack frame
pushq %rbp
movq %rsp, %rbp
# shift rsp down 48 bytes for locals
subq $48, %rsp
# locals are at rbp -4 to -32
movl $1, -4(%rbp) # a=1
movl $2, -8(%rbp) # b=2
movl $3, -12(%rbp) # c=3
movl $4, -16(%rbp) # d=4
movl $5, -20(%rbp) # e=5
movl $6, -24(%rbp) # f=6
movl $7, -28(%rbp) # g=7
movl $8, -32(%rbp) # h=8
# call sub(a,b,c,d,e,f,g,h): first 6 parameters in registers
movl -24(%rbp), %r9d # f in r9
movl -20(%rbp), %r8d # e in r8
movl -16(%rbp), %ecx # d in ecx
movl -12(%rbp), %edx # c in edx
movl -8(%rbp), %esi # b in esi
movl -4(%rbp), %eax # a in eax but will be in edi
# push 2 extra parameters h,g on stack
movl -32(%rbp), %edi # int h in edi
pushq %rdi # pushQ rdi ; only low 32-bits = h
movl -28(%rbp), %edi # int g in edi
pushq %rdi # pushQ rdi ; low 32-bits = g
movl %eax, %edi # parameter a in edi
call sub # call sub(a,b,c,d,e,f,g,h)
addq $16, %rsp # pop stack: h,g, 16 bytes
movl %eax, -36(%rbp) # i = sub return value in eax
movl $0, %eax # return 0 to crt0.o
leave
ret
|