Question: Here is an attempt at writing an assembly code version of the coin toss program. The loop control is a little different, but we are
Here is an attempt at writing an assembly code version of the coin toss program. The loop control is a little different, but we are accomplishing the same task. Copy this program over to the CS Linux Server into your labprogB directory. Using the debug switch, g assemble and link the code into an executable program. Run the program and check results.
Oh no We got the dreaded Segmentation fault! Where did it come from and what can we do about it There is something horribly wrong about this program. Do you see the error in the code? Probably not.
Of course, I am going to ask you to start the debugger. Set your first breakpoint at start.
Hint, there is exactly one line of code missing. All other lines that are given are without error.
Debug the assembly language code, add the missing line of code, and rebuild a working executable file.
Source Code
coin.s
coin toss program, written in assembly
different coding approach than the coin.c program
text
global start
start:
begin fortype loop control: loop times
mov x # preload countdown counter with
looptop:
call coinflip function
bl coinflip branch w return address saved in link register
loop control
sub x x # decrement the loop counter by one
cbnz x looptop compare counter and jump back to top if not zero
exit:
return to the operating system
mov x # system call number for exit
mov x # set return code
svc # call the kernel
coinflip:
coin flip function
generate random number and store results in 'buffer'
mov x # system call number for get random number
ldr xbuffer set address to the start of buffer memory
mov x # set buffer size to bytes
mov x # set flags to
svc # call the kernel
set the address for the output string
ldr xstrheads preload string address for 'heads'
ldrb wx read first byte random number from buffer
and w w # mask off all bits other than bit
cbnz x write compare bit and branch if to write
ldr xstrtails replace string address with 'tails'
write:
print string to the terminal
mov x # system call number for write
mov x # set to file # standard output
mov x # write six characters
svc # call the kernel
coilflipreturn:
return to calling function
br lr branch using the address in link register
data
strheads:
asciz "heads
strtails:
asciz "tails
buffer:
octa xxxx
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
