We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I'm trying to translate this code: randomInteger =(((randomInteger * 214013) + 2531011) >> 16) & 0x7FFF ;
into assembly in order to create a random integer generator, I'm guessing I'm needing the LSR, AND or MOV instructions, but I'm not sure.
I'm in an introduction to ARM Assembly Language course, I'd appreciate any help
again, there is no such thing as random generator code, only pseudorandom.
a good way to get a truly random value is to have a free running timer and reading it on some external event
Erik
Well you could build a list of expected values with a C application on the PC.
For the range you need to perform a %= 215 operation on the values.
Hi, thanks so much for the response, here's what I've come up with:
SRAM_BASE EQU 0x40000000 ;static RAM location AREA StrCopy,CODE,READONLY ENTRY LDR r1,=randomInteger ;source LDR r0,=SRAM_BASE ;destination MOV r3,#20 ;counter LDR r4,=214013 LDR r5,=2531011 random CMP r3,#0 ;check for zero terminator BLE out1 ;branch if no more out of loop LDRH r2,[r1],#2 ;load word from source, update address MUL r6,r2,r1 MOV r2,r6 ADDS r2,r5 LSRS r2,#16 LDR r5,=0x7FFF ANDS r2,r5 STRH r2,[r0],#2 ;write halfword to destination, update address SUB r3,r3,#1 ;decrement counter B random out1 stop B stop randomInteger DCW 0x1234 ;seed value END
though I'm looking at the memory and I'm not certain I'm getting what I'm supposed to be getting. The basic objective is to write an ARM assembly program that will generate 20 pseudo-random integers in the range 0 to 215-1 and store the numbers in 20 consecutive halfword locations beginning at address 0x40000000.
; randomInteger =(((randomInteger * 214013) + 2531011) >> 16) & 0x7FFF ; ldr r1, =214013 ldr r2, =2531011 ldr r3, =randomInteger ldr r0, [r3] ; load from randomInteger mul r0, r1 ; *= 214013 adds r0, r2 ; += 2531011 lsrs r0, #16 ; >>= 16 ldr r2, =0x7FFF ands r0, r2 ; &= 0x7FFF str r0, [r3] ; store to randomInteger ; ... ; data section randomInteger dcd 0x1234