Reputation: 23
I'm currently working on an assembly project right now and I have to generate a random number. So far I've managed to write some code that should work, but I don't think is working beacuse it does not print the random number. My code is this:
/*--random01.s*/
.data
.balign 4
mensaje1: .asciz "Random \n"
.balign 4
return: .word 0
.text
.global main
main:
ldr r1, addr_of_return
str lr, [r1]
ldr r0, addr_of_msg1
bl printf
tst r1,r1, lsr #1
movs r2,r0, rrx
adc r1,r1, r1
eor r2,r2, r0, lsl #12
eor r0,r2, r2, lsr #20
ldr r0, [r0]
bl printf
ldr lr, addr_of_return
ldr lr, [lr]
bx lr
addr_of_msg1: .word mensaje1
addr_of_return: .word return
.global printf
My question is, how can I achieve the generation of a random number? Am I doing this right or should I try working with the clock?
Upvotes: 1
Views: 7099
Reputation: 282
Here are some notifications to your program to get it to print if you wish to try more options.
/*--random01.s*/
.data
.balign 4
mensaje1: .asciz "Random \n"
@ ---- Added ----
.balign 4
format: .asciz "%d \n"
@ ---------------
.balign 4
return: .word 0
.text
.global main
main:
ldr r1, addr_of_return
str lr, [r1]
ldr r0, addr_of_msg1
bl printf
tst r1,r1, lsr #1
movs r2,r0, rrx
adc r1,r1, r1
eor r2,r2, r0, lsl #12
eor r0,r2, r2, lsr #20
@ ---- Added ----
mov r1, r0 @ num goes to r1
ldr r0, addr_of_format @ format to r0
@ ---------------
@ ldr r0, [r0]
bl printf
ldr lr, addr_of_return
ldr lr, [lr]
bx lr
addr_of_msg1: .word mensaje1
addr_of_return: .word return
@ ---- Added ----
addr_of_format: .word format
@ ---------------
.global printf
Here is a program that reads one byte from /dev/urandom and prints it to stdout. The number is 0 to 255 (one of those numbers or between them). Some reference is http://cseweb.ucsd.edu/~swanson/papers/Oakland2013EarlyEntropy.pdf. I did not understand most of it.
@----------------------------------
.data
@ See /usr/include/arm-linux-gnueabihf/asm/unistd.h
@ See /usr/include/arm-linux-gnueabihf/bits/fcntl-linux.h
.equ create, 8
.equ Mode, 0644 @ -rw-r--r--
.equ open, 5
.equ Rd, 00
.equ Wr, 01
.equ RdWr, 02
.equ Apnd, 02000
.equ read, 3
.equ write, 4
.equ close, 6
.equ sync, 36
.equ exit, 1
.equ sfile, 187
@----------------------------------
.balign 4
dir_file:
.asciz "/dev/urandom"
.balign 4
Open:
.word dir_file, RdWr | Apnd, open
.balign 4
Buf:
.word 0
.balign 4
Read:
.word Buf, 1, read
.balign 4
format:
.asciz "%3d\n"
@----------------------------------
.text
.global main, printf
main:
push {r4, r5, r7, lr} @ folowing AAPCS
ldr r3, =Open @ load address
ldm r3, {r0, r1, r7} @ load registers
svc #0 @ OS opens file
mov r4, r0 @ save fd in r4
ldr r3, =Read @ load address
ldm r3, {r1, r2, r7} @ load registers
svc #0 @ OS reads file
mov r0, r4 @ move fd in r0
mov r7, #close @ num for close
svc #0 @ OS closes file
ldr r0, =format @ adress of format
ldr r1, =Buf @ addr of byte red
ldr r1, [r1] @ load byte
bl printf @ C() print byte
mov r0, #0 @ 0 = success
exit:
pop {r4, r5, r7, lr} @ folowing AAPCS
bx lr @ Exit if use gcc as linker
NOTE: as to assemble, gcc to link, tested on RPi3
Upvotes: 1