Advertisement
segmentationfault

Rand.s

Mar 26th, 2021 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.96 KB | None | 0 0
  1. .global _start
  2.  
  3. /* PseudoRandom number Generator
  4.  
  5.     This pseudo random number generator takes the 4th byte from the linux gettime(2) syscall
  6.     which is the time in microseconds from epotch. This will then load the number into
  7.     r1, which then subtracts from the number until it's an integer less than 10 but greater than 0.
  8.     This subtraction is my poor mans attempt at some kind of modulus or floor operator without getting
  9.     overly complicated as I only need to print 1 digit.
  10.    
  11.     The result of the "fakemod" will then be fed into into an algorithm to convert
  12.     the integer as an ascii string for output.
  13.     The period doesn't print at the end, but everything works.
  14.    
  15.     Was constrained in terms of not being allowed to use urandom, use of urandom would not have been marked by the professor.
  16. */
  17.  
  18. _start:
  19.  
  20. MOV r5, #0
  21. Numberloop:
  22.     ADD r5, r5, #1      @r5 is my counter
  23.    
  24.     PUSH {r5}
  25.     BL timeofday        @grab our random number, return in r1
  26.     BL fakemod          @perform a (fake) mod operation, return in r1
  27.  
  28.     BL _numtoString
  29.    
  30.     LDR r1, =numstr
  31.     BL _sPrint
  32.    
  33.     LDR r1, =comma
  34.     BL _sPrint
  35.    
  36.     POP {r5}
  37.     CMP r5, #9
  38.     BNE Numberloop
  39.     BEQ lasttask
  40.    
  41. lasttask:
  42.  
  43.     BL timeofday        @grab our random number, return in r1
  44.     BL fakemod          @perform a (fake) mod operation, return in r1
  45.     BL _numtoString
  46.    
  47.     LDR r1, =numstr
  48.     BL _sPrint
  49.    
  50. @ This is here for testing purposes, originally _sPrint was a seperate library file in .o provided by the professor
  51. @ I had to disassemble the object file to extract _sPrint, _strLen, and findEnd so to avoid having to give out
  52. @ the object file. Nothing about the code behaviour has changed. A period is still not printed at the end.
  53.  
  54. @This was to see if maybe the _sPrint function being used in the object file might have been the reason for the missing period.
  55. @As at the time I did not have it disassembled
  56.  
  57.     MOV r7, #4              @set the write bit (4) in register 7 to write to console
  58.     MOV r0, #1              @set WRITE destination to STDOUT (terminal)
  59.     LDR r1, =period         @Loads data store at the address ID'd by the label, into r1 for output
  60.    MOV r2, #2              @Set R2 to be the max size output prompt.
  61.    SWI 0                   @RUN/EXECUTE WRITE syscall
  62.    
  63. @missing newline printing, commented it out for testing.
  64.     @LDR r1 =newline
  65.     @BL _sPrint
  66.  
  67.     B _exit
  68.  
  69.  
  70. @functions here
  71.  
  72. timeofday:
  73.  
  74.     MOV r7, #78         @gettimeofday returns time
  75.     LDR r0, =time       @address of holder for time_t
  76.     MOV r1, #0          @timezone not needed
  77.     SWI #0
  78.    
  79.     LDR r0, =time       @set r0 back to label addr
  80.     LDRB r1, [r0, #4]   @load epotch value from r0 into r1
  81.    
  82.     MOV PC, LR          @back to calling location
  83.  
  84. fakemod:
  85.     CMP r1, #10         @Ensuring we don't create a negative
  86.     SUBGE r1, r1, #10   @decrease r1 by 10 until it's a single digit
  87.     CMP r1, #10
  88.     BGE fakemod         @if the number in r1 is still greater than 10 loop
  89.    
  90.     MOV PC, LR          @once it's an integer less than 10, go back to calling enviroment
  91.  
  92.  
  93. _numtoString:
  94.  
  95.     ADD r1, r1, #48     @coverts an integer to an ascii value
  96.     LDR r4, =numstr     @loads the numstr pointer address to r4
  97.     STR r1, [r4]        @stores the Ascii integer representation into the numstr address
  98.  
  99.     MOV PC, LR          @so back to the BL that called this function
  100.  
  101. _sPrint:
  102.     mov r0, #1          @sets r0 to 1 for printing
  103.     push {r0, r1, lr}   @saves r0,r1 and lr before branching to _strlen
  104.     bl _strLen
  105.     pop {r0, r1, lr}    @restores the saved register
  106.     mov r7, #4          @sets output to console
  107.     SWI 0               @execute syscall
  108.     mov pc, lr          @back to call enviroment
  109.  
  110.  
  111. _strLen:
  112.   mov   r2, #0          @r2 is the counter
  113.  
  114. findEnd:
  115.  ldrb   r0, [r1], #1    @load a value from r1 into r0 and increment r1 address offset
  116.   add   r2, r2, #1      @increment counter
  117.   cmp   r0, #0          @compare to 0, end of string
  118.   bne findEnd           @if not 0, loopback
  119.   sub   r2, r2, #1      @subtract 1 so we're not one step ahead of desire pointer location
  120.   mov   pc, lr         
  121.  
  122.    
  123.    
  124.    
  125. _exit:
  126.     MOV r7, #1
  127.     SWI #0
  128.  
  129. .data
  130. time:       .space 8
  131. comma:      .asciz ", "
  132. newline:    .asciz  "\n"
  133. numstr:     .asciz " "
  134. period:     .ascii ".\n"
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement