-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_main.s
72 lines (55 loc) · 2.67 KB
/
run_main.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
.section .rodata # Read only data section
formatS: .string "%s"
formatD: .string "%d"
########
.text
.globl main
.type main, @function
main:
pushq %rbp # Save the old frame pointer.
movq %rsp, %rbp # Set %rbp to %rsp's value.
subq $528, %rsp # Allocate memory on the stack for pstring1, pstring2 and the choice of the client.
# Aloocation 528 (more then needed) becouse it's a 16 multiplication.
# receiving PS1 number:
leaq -528(%rbp), %rsi # Loading the length of pstring1 (n1) to %rsi as second argument to scanf.
movq $formatD, %rdi # Setting the format of the number (%d) to %rdi as first argument to scanf.
xorq %rax, %rax # Clearing %rax.
call scanf # Calling scanf.
# receiving PS1 string:
leaq -527(%rbp), %rsi # Loading the string of pstring1 to %rsi as second argument to scanf.
movq $formatS, %rdi # Setting the format of the string (%s) to %rdi as first argument to scanf.
xorq %rax, %rax # Clearing %rax.
call scanf # Calling scanf.
# Receiving PS2 number:
leaq -272(%rbp), %rsi # Loading the length of pstring2 (n2) to %rsi as second argument to scanf.
movq $formatD, %rdi # Setting the format of the number (%d) to %rdi as first argument to scanf.
xorq %rax, %rax # Clearing %rax.
call scanf # Calling scanf.
# Receiving PS2 string:
leaq -271(%rbp), %rsi # Loading the string of pstring2 to %rsi as second argument to scanf.
movq $formatS, %rdi # Setting the format of the string (%s) to %rdi as first argument to scanf.
xorq %rax, %rax # Clearing %rax.
call scanf # Calling scanf.
# Receiving the switch value:
leaq -16(%rbp), %rsi # Loading the number of the choice to %rsi as second argument to scanf.
movq $formatD, %rdi # Loading the format of the choice (%d) to %rdi as first argument to scanf.
xorq %rax, %rax # Clearing %rax.
call scanf # Calling scanf.
# Clear all relevant registers.
xorq %rdi, %rdi
xorq %rsi, %rsi
xorq %rdi, %rdx
# Pass caller arguments.
leaq -272(%rbp), %rdx # Set PS2 to %rdx as third argument.
leaq -528(%rbp), %rsi # Set PS1 to %rsi as second argument.
movl -16(%rbp), %edi # Set the choice to %edi as first argument.
# Clear %rax and call func_select (where the switch case is).
xorq %rax, %rax
call func_select
# Restor the alocated space.
addq $528, %rsp
#pop back %rbp and return
xorq %rax, %rax # Clear %rax.
movq %rbp, %rsp # Restore the old stack pointer - release all used memory.
popq %rbp # Restore old frame pointer (the caller function frame).
ret # Return to caller function.