56 lines
1.5 KiB
ArmAsm
56 lines
1.5 KiB
ArmAsm
# Metadata for debuggers and other tools
|
|
.global main
|
|
.type main, @function
|
|
.extern printf
|
|
.extern scanf
|
|
|
|
.section .text # Begins code and data
|
|
# Label that marks beginning of main function
|
|
main:
|
|
# Function stack setup
|
|
pushq %rbp
|
|
movq %rsp, %rbp
|
|
subq $128, %rsp # Reserve 128 bytes of stack space
|
|
|
|
# Read an integer into -8(%rbp)
|
|
movq $scan_format, %rdi # 1st param: "%ld"
|
|
leaq -8(%rbp), %rsi # 2nd param: (%rbp - 8)
|
|
callq scanf
|
|
# If the input was invalid, jump to end
|
|
cmpq $1, %rax
|
|
jne .Lend
|
|
|
|
# Read another integer into -16(%rbp)
|
|
movq $scan_format, %rdi
|
|
leaq -16(%rbp), %rsi
|
|
callq scanf
|
|
# If the input was invalid, jump to end
|
|
cmpq $1, %rax
|
|
jne .Lend
|
|
|
|
|
|
# Add the two integers together
|
|
movq -8(%rbp), %rax # Load first integer into %rax
|
|
addq -16(%rbp), %rax # Add second integer to %rax
|
|
|
|
# Call function 'printf("%ld\n", %rsi)'
|
|
# to print the number in %rsi.
|
|
movq $print_format, %rdi
|
|
movq %rax, %rsi
|
|
callq printf
|
|
|
|
# Labels starting with ".L" are local to this function,
|
|
# i.e. another function than "main" could have its own ".Lend".
|
|
.Lend:
|
|
# Return from main with status code 0
|
|
movq $0, %rax
|
|
movq %rbp, %rsp
|
|
popq %rbp
|
|
ret
|
|
|
|
# String data that we pass to functions 'scanf' and 'printf'
|
|
scan_format:
|
|
.asciz "%ld"
|
|
print_format:
|
|
.asciz "%ld\n"
|