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 lab_5/prog_7B 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 for-type loop control: loop 5-times
mov x19, #5// preload count-down counter with 5
loop_top:
// call coin_flip function
bl coin_flip // branch w/ return address saved in link register
// loop control
sub x19, x19, #1// decrement the loop counter by one
cbnz x19, loop_top // compare counter and jump back to top if not zero
exit:
// return to the operating system
mov x8, #93// system call number for exit
mov x0, #0// set return code =0
svc #0// call the kernel
coin_flip:
// coin flip function
// generate random number and store results in 'buffer'
mov x8, #278// system call number for get random number
ldr x0,=buffer // set address to the start of buffer memory
mov x1, #4// set buffer size to 4 bytes
mov x2, #0// set flags to 0
svc #0// call the kernel
// set the address for the output string
ldr x1,=str_heads // preload string address for 'heads'
ldrb w2,[x0]// read first byte (random number) from buffer
and w2, w2, #1// mask off all bits other than bit 0
cbnz x2, write // compare bit and branch if 1 to write
ldr x1,=str_tails // replace string address with 'tails'
write:
// print string to the terminal
mov x8, #64// system call number for write
mov x0, #1// set to file #1, standard output
mov x2, #6// write six characters
svc #0// call the kernel
_coil_flip_return:
// return to calling function
br lr // branch using the address in link register
.data
str_heads:
.asciz "heads
"
str_tails:
.asciz "tails
"
buffer:
.octa 0x0,0x0,0x0,0x0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!