Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .global isNumber
- .data
- .text
- isNumber:
- mov r12,r13 // save stack pointer into register r12
- sub sp,#32 // reserve 32 bytes of space for local variables
- push {lr} // push link register onto stack -- make sure you pop it out before you return
- // char c is stored in R2
- // ASCII digits are stored in decimal values 48-57
- // We will first check to see if R2 is equal to or greater than 48
- // If the first check is passed, we will then check to see if R0 is
- // less than or equal to 57
- checkOne: // If both checks are passed, c is a digit
- CMP R2, #48 // Compare R2 and 48
- BGE checkTwo // If R2 is greater than or equal to 48, continue to check two
- B isNotDigit // Else c is not a digit
- checkTwo:
- CMP R2, #57 // Compare R2 and 57
- BLE isDigit // If R2 is less than or equal to 57, c is a digit
- B isNotDigit // Else c is not a digit
- isDigit:
- mov R0, #1 // Sets return value to 1 because c is a digit
- b isNumberExit // Jumps to end of function
- isNotDigit:
- mov R0, #0 // Sets return value to 0 because c is not a digit
- b isNumberExit // Jumps to end of function
- isNumberExit:
- pop {lr} // pop link register from stack
- mov sp,r12 // restore the stack pointer -- Please note stack pointer should be equal to the
- // value it had when you entered the function .
- mov pc,lr // return from the function by copying link register into program counter
- .global compare
- .data
- .text
- compare:
- mov r12,r13 // save stack pointer into register r12
- sub sp,#32 // reserve 32 bytes of space for local variables
- push {lr} // push link register onto stack -- make sure you pop it out before you return
- // int a stored in R0
- // int b stored in R1
- CMP R0, R1 // Compares a and b
- BGT greaterThan // Jumps to greaterThan branch if R0>R1
- BEQ equalTo // Jumps to equalTo branch if R0=R1
- BLT lessThan // Jumps to lessThan branch if R0<R1
- greaterThan:
- MOV R0, #1 // Sets return value to 1 if R0>R1
- b compareExit // Jumps to end of function
- equalTo:
- MOV R0, #0 // Sets return value to 0 if R0=R1
- b compareExit // Jumps to end of function
- lessThan:
- MOV R0, #-1 // Sets return value to -1 if R0<R1
- b compareExit // Jumps to end of function
- compareExit:
- pop {lr} // pop link register from stack
- mov sp,r12 // restore the stack pointer -- Please note stack pointer should be equal to the
- // value it had when you entered the function .
- mov pc,lr // return from the function by copying link register into program counter
- .global countOnes
- .data
- .text
- countOnes:
- mov r12,r13 // save stack pointer into register r12
- sub sp,#32 // reserve 32 bytes of space for local variables
- push {lr} // push link register onto stack -- make sure you pop it out before you return
- // int number stored in R0
- MOV R1, #0 // loopCount stored in R1
- MOV R2, #0 // onesCount stored in R2
- MOV R3, #0 // tempVal stored in R3
- countOnesLoop:
- CMP R1, #32 // Compare R1 and 32
- BGE exitCountOnes // Exit if loopCount>32
- MOV R3, R0 // Copy R0 into R3 so we can AND it
- AND R3, #1 // And R3 with 1 to see if bit is 1 or 0
- ADD R2, R3 // Add bit, either one or zero, to onesCount
- LSR R0, #1 // Shift R0 right 1
- ADD R1, #1 // Increment loopCount by 1
- b countOnesLoop // Continue looping
- exitCountOnes:
- MOV R0, R2 // Set return value to onesCount
- pop {lr} // pop link register from stack
- mov sp,r12 // restore the stack pointer -- Please note stack pointer should be equal to the
- // value it had when you entered the function .
- mov pc,lr // return from the function by copying link register into program counter
- .global returnHammingDistance
- .data
- .text
- returnHammingDistance:
- mov r12,r13 // save stack pointer into register r12
- sub sp,#32 // reserve 32 bytes of space for local variables
- push {lr} // push link register onto stack -- make sure you pop it out before you return
- // char firstparameter stored in R0
- // char secondparameter stored in R1
- MOV R2, #0 // loopCount stored in R2
- MOV R3, #0 // hammingDistance stored in R3
- MOV R4, #0 // tempValOne stored in R4
- MOV R5, #0 // tempValTwo stored in R4
- hammingDistanceLoop:
- CMP R2, #32 // Compare R2 and 32
- BGE exitHammingDistance // Exit if loopCount>32
- MOV R4, R0 // Copy R0 into R4 so we can AND it
- MOV R5, R1 // Copy R1 into R5 so we can AND it
- AND R4, #1 // And R4 with 1 to see if bit is 1 or 0
- AND R5, #1 // And R5 with 1 to see if bit is 1 or 0
- CMP R4, R5
- ADDNE R3, R3, #1 //Increment hammingDistance by 1 if bits are not equal
- LSR R0, #1 // Shift R0 right 1
- LSR R1, #1 // Shift R0 right 1
- ADD R2, #1 // Increment loopCount by 1
- b hammingDistanceLoop // Continue looping
- exitHammingDistance:
- MOV R0, R3 // Set return value to hammingDistance
- pop {lr} // pop link register from stack
- mov sp,r12 // restore the stack pointer -- Please note stack pointer should be equal to the
- // value it had when you entered the function .
- mov pc,lr // return from the function by copying link register into program counter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement