General Instructions In the following pages you are request
General Instructions : In the following pages you are requested to write 4 Assembly language functions. The skeleton code for the C version of these functions is given below.
Place all Assembly functions into a file named as “Homework6_yourreadID.S” and submit this file on blackboard.
Function 1 :
int isNumber(char *c)
Function should return 1 is the argument passed is a digit and zero otherwise. Please note the asci equivalents of numeric characters from an asci table.
Please note that you will write the function in assembly. The skeleton code for an assembly language equivalent of the above function is given below as example.
.global _isNumber
_isNumber:
Pushl %ebp
Movl %esp,%ebp
// YOUR CODE HERE
Popl %ebp
Ret
Function 2 :Given the following C function prototype that accepts two integer arguments. Complete the function and return 1 if a > b , return -1 if a < b and return 0 if a is equal to b. Please write the function in assembly. Note that the assembly languge skeleton code is given in the attached .s file.
int compare(int a, int b)
Function 3 : Given the following C function prototype which accepts an integer argument, complete the implementation of the function in Assembly language to return the number of 1’s in the binary representation of the number passed. For example if the number is (011100011 ) then the function should return 5 as the number of 1s. Please remember that an integer is 32 bits long. Note that the assembly languge skeleton code is given in the attached .s file.
int countOnes(int number)
Function 4: Write an assembly language function to determine the hamming distance ( the number of different bits) between two characters passed as parameter. Please note that the function prototype is given below and the function will be implemented in assembly language. Note that the assembly languge skeleton code is given in the attached .s file.
int returnHammingDistance( char firstparameter, char secondparameter)
Solution
FUNCTION-1:
.global _isNumber
_isNumber:
pushl %rbp
movl %rsp,%rbp
subl $0x16,%rsp
movl 8(%rbp),%esi // esi = address of parameter1.
cmpb $0,(%esi) // in case no input data.
jz not_number
// if each input datas is between 48 and 57, it is number.
loop_isNumber:
cmpb $48,(%esi)
jl not_number
cmpb $57,(%esi)
jg not_number
incl %esi
// compare until found Nul, then goto number: to ret.
cmpb $0,(%esi)
jz number
jmp loop_isNumber
number:
movl $1,%eax
movl %rbp,%rsp
popl %rbp
ret
not_number:
movl %rbp,%rsp
popl %rsp
ret
FUNCTION-2:
.global _compare
_compare:
pushl %rbp
movl %rsp,%rbp
subl $0x16,%rsp
movl 8(%rbp),%ebx // ebx = parameter1.
movl 12(%rbp),%edx // edx = parameter2.
xorl %eax,%eax // reset return value.
// compare parameter1 & 2.
cmpl %ebx,%edx
jg para1_gt_para2
cmpl %ebx,%edx
jl para2_gt_para1
// they are equal then go return with %eax = 0.
jmp ret_compare
para1_gt_para2:
movl $1,%eax
jmp ret_compare
para2_gt_para1:
movl $-1,%eax
ret_compare:
movl %rbp,%rsp
popl %rsp
ret
FUNCTION-3:
.global _countOnes
_countOnes:
pushl %rbp
movl %rsp,%rbp
subl $0x16,%rsp
movl 8(%rbp),%edx // edx = parameter1.
xorl %eax,%eax // reset return value.
movl $32,%ecx. // set cx = 32 for being counter.
// loop check bit1 = 0/1.
loop_countOnes:
testl $0x1,%edx
jz not_1
// count up for bit1 = 1.
incl %eax
not_1:
shrl $1,%edx
decl %ecx
jnz loop_countOnes
movl %rbp,%rsp
popl %rsp
ret
FUNCTION-4:
.global _returnHammingDistance
_returnHammingDistance:
pushl %rbp
movl %rsp,%rbp
subl $0x16,%rsp
movl 8(%rbp),%ebx // ebx = parameter1.
movl 12(%rbp),%edx // edx = parameter2.
xorl %eax,%eax // rest return value.
movl $8,%ecx // set cx = 8 for being counter.
loop_HammingDistance:
testl $0x1,%ebx
jnz case_bit1para1_1
// case : bit 1 of parameter1 = 0
testl $0x1,%edx
jz bit_equal
// case : bit 1 of parameter2 = 1
jmp bit_different
case_bit1para1_1:
// case : bit 1 of parameter1 = 1
testl $0x1,%edx
jnz bit_equal
// case : bit 1 of parameter2 = 0
bit_different:
incl %eax
bit_equal:
shrl $1,%ebx
shrl $1,%edx
decl %ecx
jnz loop_HammingDistance
movl %rbp,%rsp
popl %rsp
ret




