I trying to make a random number generator made using assembly. To make the file and run it I’m using vim and gcc but I keep getting a segmentation fail.
.section .data
seed: .word OxA5A5A5A5 // Initial seed for
the LFSR
.section .text .global main
main:
// Start and initialization of the seed
LDR RO, =seed
// Load address of
seed into RO
LDR R1, [RO]
// Load seed value into
R1
generate_random:
// Perform LFSR
LSR R2, R1, #1
// Logical shift right
AND R3, R1, #1
// AND with 1 to get
LSB
EOR R2, R2, R3, LSL #31 // XOR with LSB shifted to MSB
MOV R1, R2
// Move result back to
R1
// Store new seed
STR R1, [RO]
// Store updated seed
back to memory
// Use lower bits of the seed to
determine LED output
AND R4, R1, #0xFF // Mask lower 8 bits
BL set_leds
function
// Branch to set_leds
wait_button:
// Wait for button press
LDR R5, =0xFE200000 // Base address
ADD R6, R5, #0x34 // GPIO Pin Level Register
LDR R6, [R6] // Load GPIO level register
LDR R7, =0x0400
// Load constant (1
<< 10) into R7
AND R6, R6, R7
// Check GPIO pin 10
(button)
BEQ wait_button
// Loop until button
is pressed
// Delay loop for debouncing
MOV R7, #0x3F0000 // Load delay value
delay_loop:
SUBS R7, R7, #1 // Decrement delay
counter
BNE delay_loop
finished
// Loop until delay is
B generate_random // Go back to generate_random
// Function to set the LEDs
set_leds:
PUSH {R4-R7, LR} // Save the return
address and used registers
// Setup for the GPIO
LDR R6, =0xFE200000 // Base address for GPIO on Raspberry Pi 4
// Set GPIO pins 2-9 to output
LDR R7, =0x000003FC // Load constant (0b11111111 << 2) into R7
STR R7, [R6, #0x04] // Set as output
// Clear the GPIO pins
LDR R7, =0x000003FC // Load constant
(Ob11111111 << 2) into R7
STR R7, [R6, #0x28] // Clear the pins
// Set the GPIO pins based on R4
MOV R7, R4
AND R7, R7, #0xFF
LDR R8, =0x000000FC // Load constant
(0b11111111 << 2) into R8
ORR R7, R7, R8
STR R7, [R6, #0x1C] // Set the pins
POP {R4-R7, LR} // Restore the registers and return address
BX LR
// Return from function
I ran the gdb to see were its failing at set_leds and backtraces to generate_random. I
have also ran a Python script to check my GPIO pins and they were good. I have also try remaking the whole file and directory and using small bits of code to try and troubleshoot it.
New contributor
Raging Storm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.