Question
Assembly Language, my code keeps giving me segmentation faults, can you please help me to fix it? // utility division functions .global div_mod .global divide
Assembly Language, my code keeps giving me segmentation faults, can you please help me to fix it?
// utility division functions .global div_mod .global divide .global mod .global main
/* Raspberry Pi 4 GPIO base addresses */ .equ GPIO_BASE, 0xFE200000 .equ GPIO_CTRL, 0x00 .equ GPIO_ON, 0x1C .equ GPIO_OFF, 0x
/* Define constants for LCG */ .equ RAND_MAX, 255 /* Define the maximum value for the random number */
/* Constants for LCG */ .equ a, 1103515245 .equ c, 12345 .equ m, 32768
/* Function to initialize GPIO pins */ init_gpio: // ldr r1, =GPIO_BASE ldr r1, =OUTPUT mov r0, #17 /* GPIO pin 17 */ lsl r0, r0, #2 /* Multiply by 4 for register offset */ str r0, [r1, #GPIO_CTRL] /* Set pin 17 as output */ mov r2, #27 lsl r2, r2, #2 str r2, [r1, #GPIO_CTRL] mov r3, #22 lsl r3, r3, #2 str r3, [r1, #GPIO_CTRL] mov pc, lr /* Return */
/* Function to generate a random number */ generate_random_number: push {lr} ldr r1, =a ldr r1, [r1] mul r0, r1, r0 /* a * Xn */
ldr r1, =c ldr r1, [r1] add r0, r0, r1 /* a * Xn + c */
ldr r1, =m ldr r1, [r1]
bl mod //sdiv r0, r2, r1 /* (a * Xn + c) % m */
mov r0, r0, lsr #16 /* Consider only the lower 16 bits for the random number */ and r0, r0, #RAND_MAX /* Limit the range of the random number */ pop {pc}
/* Function to light up LEDs based on the generated random number */ light_up_Leds: push {lr} bl generate_random_number /* Get a random number */ //ldr r1, =GPIO_BASE /* Load GPIO base address */ ldr r1, =OUTPUT ldr r2, =17 /* LED GPIO pin number (17) */ lsl r2, r2, #2 /* Multiply by 4 for register offset */ str r2, [r1,# GPIO_CTRL] /* Set pin as output */
cmp r0, #0 /* Check if random number is zero */ beq skip_LED_on /* If zero, skip turning LED on */
str r2, [r1, #GPIO_ON] /* Turn LED on */ b end_light_up /* End of function */
skip_LED_on: str r2, [r1, #GPIO_OFF] /* Turn LED off */ str r3, [r1, #GPIO_OFF] str r4, [r1, #GPIO_OFF] end_light_up: pop {pc} /* Return */
/* Main function */ main: push {lr}
bl init_gpio /* Initialize GPIO pins */ loop: bl light_up_Leds /* Control LEDs based on random number */ b loop /* Loop indefinitely */
.func div_mod // div_mod - divide and modulus of ( a % b and a / b ) // input: R0: a // R1: b // returns: R0: a % b // R1: a / b div_mod: push {lr} mov r3, r1 mov r1, #0 mov r2, #1
_div_mod_done: // R0 = a % b, R1 = a / b pop {pc} .endfunc
.func divide // divide - divides two unsigned integers (simply calls div_mod and places respective return value in R0) // input: R0: a // R1: b // returns: R0: a / b divide: push {lr} bl div_mod mov r0, r1 // divide returns a / b pop {pc} .endfunc
.func mod // mod - gets the modulus of two unsigned integers (simply calls div_mod - this function may not be needed since div_mod already // returns R0 as a %b, but is provided anyways for completeness) // input: R0: a // R1: b // returns: R0: a % b mod: push {lr} bl div_mod pop {pc} .endfunc
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started