Last active
August 31, 2020 22:33
-
-
Save juliavdkris/8f1cb1c5c5f3c28fafc190b461dc16dc to your computer and use it in GitHub Desktop.
CO lab course oddify source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assemble with: | |
# gcc -no-pie -g -o oddifier oddifier.s | |
# From: CSE1400 Lab Course Manual | |
# Delft University of Technology | |
.text | |
welcome: .asciz "\nWelcome to our program!\n" | |
prompt: .asciz "\nPlease enter a positive number:\n" | |
input: .asciz "%ld" | |
output: .asciz "The result is: %ld\n\n" | |
.global main | |
main: | |
movq %rsp, %rbp # initialize the base pointer | |
movq $0, %rax # no vector registers in use for printf | |
movq $welcome, %rdi # first parameter: welcome string | |
call printf # call printf to print welcome | |
call inout # call the subroutine inout | |
end: # this loads the program exit code and exits the program | |
movq $0, %rdi | |
call exit | |
# *************************************************************************** | |
# • Subroutine: inout * | |
# • Description: this subroutine takes an integer as input from a user, * | |
# • increments it by 1 if it is even, and prints it out * | |
# • Parameters: there are no parameters and no return value * | |
# *************************************************************************** | |
inout: | |
# prologue | |
pushq %rbp # push the base pointer | |
movq %rsp , %rbp # copy stack pointer value to base pointer | |
movq $0, %rax # no vector registers in use for printf | |
movq $prompt, %rdi # paraml: prompt string | |
call printf # call printf to print prompt | |
subq $8, %rsp # reserve space in stack for the input | |
movq $0, %rax # no vector registers in use for scanf | |
movq $input , %rdi # param1: input format string | |
leaq -8(%rbp), %rsi # parameter2: address of the reserved space | |
call scanf # call scanf to scan the users input | |
popq %rsi # pop the input value into RSI | |
# (RSI is the second parameter register) | |
movq %rsi, %rax # copy the input to RAX | |
movq $2, %rbx # move the value 2 to RBX | |
movq $0, %rdx # clear the contents of RDX | |
divq %rbx # divide the content of RDX:RAX by the | |
# content of RB (result stored | |
# in RAX and remainder in RDX) | |
cmpq $0, %rdx # compare RDX to 0 | |
jne odd # if they are not equal (input is odd), | |
# don't increment | |
even: | |
incq %rsi # increment the input value | |
odd: | |
movq $0, %rax # no vector registers in use for printf | |
movq $output, %rdi # paraml: output string | |
# param2: number (in RSI) | |
call printf # call printf to print the output | |
# epilogue | |
movq %rbp , %rsp # clear local variables from stack | |
popq %rbp # restore base pointer location | |
ret # return from subroutine |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment