Question: With this program with the solution you provided, I get these mismatch errors, please can you fix them? asm - test.s: Assembler messages: asm -
With this program with the solution you provided, I get these mismatch errors, please can you fix them?
asmtest.s: Assembler messages: asmtest.s:: Error: operand type mismatch for shl asmtest.s:: Error: operand type mismatch for shr
Here is the code:
section data
limit: long # upper limit for primes
primecount: long # count of primes found
newline: asciz
outputfmt: asciz d
arraysize: long # size of the array
section bss
lcomm primes, # allocate bits bytes
section text
global start
# Initialize primes array and setup
start:
# Prompt for limit optional
# Initialize array to all zeros assume all are prime
movq $rax
movq $primes, rdi
movq $rcx # bytes for bits
initloop:
movb $rdi # clear byte
incq rdi
loop initloop
# Mark multiples of even numbers
movq $rdi # start from
call markmultiples
# Begin sieve, marking multiples of each found prime
movq $rdi # start from
sieveloop:
cmpq limitrdi # if current prime limit stop
jge donesieve
call isprime # check if number is prime
testq rax, rax
jz notprime
call markmultiples # mark all multiples of this prime
notprime:
addq $rdi # increment to next odd number
jmp sieveloop
donesieve:
# Print the first primes
movq $rdi # start index for printing
movq $rbx # reset prime count
printprimes:
call isprime
testq rax, rax
jz skipprint
movq rdi, rsi # pass number to printf
movq $outputfmtrdi
call printf
incq rbx # increment prime count
cmpq $rbx # stop at primes
je done
skipprint:
incq rdi # next number
cmpq limitrdi # check if within limit
jl printprimes
done:
# Exit program
movq $rax # sysexit system call
xorq rdi, rdi # status
syscall
# Function to mark multiples of a given number as nonprime
# Input: rdi current prime
markmultiples:
movq rdi, rax
imulq rdi, rax # start with square of rdi
markloop:
cmpq limitrax # stop if multiple exceeds limit
jge endmark
call clearbit # mark as nonprime
addq rdi, rax # next multiple
jmp markloop
endmark:
ret
# Function to clear bit for a nonprime number
# Input: rdi index to clear
clearbit:
movq rax, rcx
shrq $rcx # divide by to get byte index
movq rax, rdx
andq $rdx # modulus for bit position
movb $bl
shlq rdxrbx # shift by rdx positions
orq rbx primesrcx # set bit to mark as nonprime
ret
# Function to check if number is prime bit is clear
# Input: rdi number to check
# Output: rax if prime, if not prime
isprime:
movq rdi, rax
shrq $rcx # divide by for byte index
movq rdi, rdx
andq $rdx # modulus for bit position
movb primesrcxbl
shrq rdxrbx # shift bit down
andq $rbx # isolate target bit
xorq rax, rax # assume nonprime
testq rbxrbx
setz al # if bit clear, set rax to
ret
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
