Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ive seen this question posted here a bit but none of the answers are working in

ID: 3834835 • Letter: I

Question

Ive seen this question posted here a bit but none of the answers are working in spim. Please check to see if it assembles properly

In MAL an integer is represented using 32 bits. Assume that the bits are numbered right to left from 0 to 31. Thus, the rightmost bit is numbered 0 and the leftmost bit is numbered 31. We say that the bits 0 through 15 form the right half and the bits 16 through 31 form the left half of the integer.

You are required to write a MAL program that prompts a user for a positive decimal integer[1], reads the integer typed by the user and outputs the following values:

The total number of 1's in the right half of the binary representation of the integer.

The total number of 0's in the left half of the binary representation of the integer.

The highest power of 2 that evenly divides the integer[2].

The value of the largest digit in the decimal representation of the integer.

Example: Suppose the user types the decimal integer 1536. The 32-bit binary representation of 1536 is as follows:

0000 0000 0000 0000 0000 0110 0000 0000

For this example, the required answers are as follows:

The number of 1's in the right half of the binary representation of the given integer = 2.

The number of 0's in the left half of the binary representation of the given integer = 16.

The largest power of 2 that evenly divides the given integer = 9[3].

The value of the largest digit in the decimal representation of the given integer = 6.

Program outline:

The outline for your program for Part (a) must be the following.

Prompt the user for a positive integer.

Read the integer.

Compute the four quantities mentioned above and print the answers.

Stop.

Programming Suggestions:

MAL program must be in a file named p5a.mal.

Each time your program for Part (a) is executed, it should handle just one integer.

You may assume that the value typed by the user is a positive decimal integer. Thus, there is no need to do any error checking in this part.

There is no need to convert the integer to binary; when the integer is read in (using syscall), it is already in binary form.

Use bitwise operations to count the number of 1’s (0’s) in the right (left) half.

To find the highest power of 2 that divides the integer, count the number of 0’s at the end of the binary representation or use successive divisions by 2.

To extract the decimal digits and compute the largest digit, use successive divisions by 10.

[1] A positive integer is one that is strictly greater than zero.

[2] An integer x evenly divides an integer y if the remainder when y is divided by x is zero.

[3] The largest power of 2 that evenly divides 1536 is 9 because 1536 = 3 * 512 = 3 * 2^9. This highest power is also the number of 0's at the end of the binary representation of 1536.

Explanation / Answer

p5a.mal:


.data                  

instr: .asciiz "Please enter an integer: " #The prompt to enter an integer
ones:   .asciiz "Number of leading ones are: "   #The output for negative numbers shown here
zeroes:   .asciiz   "Number of leading zeroes are: " #The output for positive numbers shown here
nline: .asciiz " "               #For New line character
.text
.globl main

main:   li   $11,-1               #11 has -1 for later checking
   move   $10,$0               #Here $10 will be used to count leading numbers
  

   la $a0,instr           #To load input sting to $a0       
li $v0,4               #To load 4 to print string
   syscall

  
   li   $v0,5               #To load 5 to read int
syscall                   #The syscall v0 has input int


   move   $12,$v0               #To move the int to 12  
   beqz   $12,zero           #To check if input is zero if so jump to label zero

   beq   $12,$11,negone           #To check if input is a negative one if so jump to negone

   bgtz   $12,pos               #To check if the int is greater than zero if so jump to pos
   bltz   $12,nega           #To check if the int is less than zero if so jump to nega


zero:   la   $a0,zeroes           #lTo oad the output for positive numbers
   li   $v0,4
   syscall
   li   $a0,32               #The leading zeroes for the number zero is 32
   li   $v0,1               #command to print an int
   syscall
   la   $a0,nline           #To print newline character
   li   $v0,4
   syscall
   li   $v0,10
   syscall                   #end program


negone: la   $a0,ones           #To load the output string for negative number
   li   $v0,4
   syscall
   li   $a0,32               #the leading ones for the number negative one is 32
   li $v0,1 #command to print an int
syscall
la $a0,nline #To print newline character
li $v0,4
syscall
li $v0,10
syscall


pos:   andi   $13,$12,0x80000000       #This will get the first bit of the int and stores in 13
   bne   $13,$0,endp           #To check if 13 is not zero if so jump to end sequence for positive numbers otherwise add 1 to count
   addi   $10,$10,1          
   sll   $12,$12,1           #To shift the number by one bit to the left and jump back to pos loop
   b   pos              
  
endp: la $a0,zeroes #To load the output for positive numbers
    li $v0,4
    syscall
move   $a0,$10 #To move the count value to a0 for syscall
li $v0,1 #command to print an int
syscall
la $a0,nline #To print newline character
li $v0,4
syscall
li $v0,10
syscall


nega: andi $13,$12,0x80000000 #This will get the first bit of the int and stores in 13
beq   $13,$0,endn   #check if 13 is zero if so jump to the end sequence for negative numbers otherwise add 1 to count
addi $10,$10,1   
sll $12,$12,1 #To shift the number by one bit to the left and jump back to nega loop
b nega

endn: la $a0,ones     #To load the output for negative numbers
li $v0,4
syscall
move $a0,$10 #To move the count value to a0 for syscall
li $v0,1 #command to print an int
syscall
la $a0,nline #To print newline character
li $v0,4
syscall
li $v0,10
syscall

p5b.mal :

       .data

input: .asciiz       "Please Enter the string: "   #To print string for getting string
pinput: .asciiz       "Please Enter the patter: "   #To print string for getting pattern
nline: .asciiz       " "           #A new line character
nmatch:       .asciiz       "No match was found " #When string for no match
match:       .asciiz       "Match was found "   #a match was found string
parmatch:   .asciiz       "Length of the partial match: "   #For string to print for partial match
indexstring:   .asciiz       "Starting index = "   #For string to print before printing index of match
string:       .byte       0:82           #A char array for 81 chars+ ''
pattern:   .byte       0:12           #A char array for 11 chars+ ''  

       .text
       .globl main


main:   la   $a0,input   #To print the prompt for string
   li   $v0,4
   syscall
   li   $v0,8       #To get string and store in string array
   la   $a0,string
   li   $a1,82
   syscall
   la $a0,pinput   #To print the prompt for pattern
li $v0,4
syscall
li $v0,8       #To store the pattern in the pattern array
la $a0,pattern
li $a1,12
syscall

   jal   pattern_match   #A procedure call to pattern matching function
  
   li   $15,2       #To load 2 to 15 for checking for partial matching
   beqz   $12,nomatch   #if 12 has 0 then no match occured so jump to nomatch
   beq   $15,$12,pmatch   #if 12 has 2 then a partial match occured so jump to pmatch
   la   $a0,match   #otherwise a full match occured so load the string to print for match
   li   $v0,4
   syscall
   la   $a0,indexstring   #To print the index string
   li   $v0,4
   syscall
   move   $a0,$14       #To move the value in 14(the counter) to a0 and print it
   li   $v0,1
   syscall
   la   $a0,nline   #To print newline
   li   $v0,4
   syscall
   li   $v0,10       end program
   syscall

pmatch:   la   $a0,nmatch   #To print the no match string
   li   $v0,4
   syscall
   la   $a0,parmatch   #To print the partial match length string
   li   $v0,4
   syscall
   move   $a0,$16       #To print the value in 16 which is the length of the match
   li   $v0,1
   syscall
   la   $a0,nline   #To print a new line
   li   $v0,4
   syscall
   la   $a0,indexstring
   li   $v0,4
   syscall
   move   $a0,$14       #To print the address
   li   $v0,1
   syscall
   la   $a0,nline
   li   $v0,4
   syscall
   li   $v0,10       #end program
   syscall
  

nomatch:la   $a0,nmatch   #To print the no match string
   li   $v0,4  
   syscall
   li   $v0,10       #end program
   syscall


pattern_match:
   move   $14,$0       #$14 holds the index
   li   $13,' '   #$13 has newline char to check for end of arrays
   la   $8,string   #$8 has the string
   la   $9,pattern   #$9 has the pattern
   move   $12,$0       #This holds flag for various conditions
   move   $16,$0       #The length counter
  
while:   lbu $10,0($8) #$10 has the first byte(first char) from string
lbu $11,0($9) #$11 has the first byte(first char) from the pattern
   beq   $10,$13,endp   #To check to see if the string reached the end
   beq   $11,$13,reset   #if the pattern string reaches end jump to reset
   bne   $11,$10,missmatch   #if the two characters dont match jump to mismatch
   beq   $10,$11,matched   #if the characters match jump to matched


matched:
       addi   $16,$16,1   #To increment the length
       addi   $8,$8,1       #To increment the string address
       addi   $9,$9,1       #To increment the pattern address
       li   $12,1       #To set 12 to 1 meaning match so far
       j   while       #This is back to the while loop

missmatch:     li   $15,1       #To load 1 to 15 so we can check if there was a match before this point, this is used for partial match
       beq   $12,$15,end2   #if 12 had 1, meaning up till now there was a match of atleast length 1, jump to end2
       addi $8,$8,1    #otherwise increment the address of the string to get next character
addi $14,$14,1 #increment the counter
b while    #This is to go back to the while loop

end2:   li   $12,2           #Now we load 2 into 12 to say there was a partial match(this gets checked in the main)
   jr   $ra           #To return to main
  
endp:   jr   $ra           #Toreturn to main without changing any flags
  

reset:   li   $15,1           #Now we load 1 to 15 to check in next instruction
   beq   $12,$15,endp       #we check if there were any matches before this point if there is we end the function
   la $9,pattern       #if there was not we reload the address of pattern back into 9 to keep checking agasint string
   j while           #and jump back to the while loop