public
Created

  • Download Gist
gistfile1.asm
Assembly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
[bits 64]
 
extern rand
extern time
extern srand
extern write
global main
 
section .text
%define SIZE (31*2048)
 
main:
; Initialize srand
xor rdi, rdi
call time
mov rdi, rax
call srand
 
; rbp will always point to the buffer
mov rbp, buf
 
loop_outer:
; Zero out rbx
xor ebx, ebx
 
loop_inner:
; Get a random 31-bit number into rax
call rand
 
; Put the random bits into xmm1 and xmm2
movd xmm1, eax
movd xmm2, eax
 
; Put the lower 16 bits in xmm1, and the upper 15 in xmm2
pshufb xmm1, [shuffler1]
pshufb xmm2, [shuffler2]
 
; Zero out xmm0
pxor xmm0, xmm0
 
; Put a single bit from the random number into each byte of xmm0/xmm1
pand xmm1, [and1]
pand xmm2, [and1]
 
; Set each byte to 0xff if the bit was 0, otherwise 0
pcmpeqb xmm1, xmm0
pcmpeqb xmm2, xmm0
 
; Mask out the difference between '/' and '\'
pand xmm1, [and2]
pand xmm2, [and2]
 
; Add the value for '/', such that each byte is now either '/ or '\'
paddb xmm1, [adder]
paddb xmm2, [adder]
 
; Save the 31 bytes
movdqu [rbp + rbx], xmm1
movdqu [rbp + rbx + 16], xmm2
add ebx, 31
 
; Check if done
cmp ebx, SIZE
jne loop_inner
 
; Print them
mov rdi, 1
mov rsi, rbp
mov rdx, SIZE
call write
 
; Go again
jmp loop_outer
 
 
section .data
align 16
shuffler1: db 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
shuffler2: db 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3
and1: db 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80
and2: times 16 db 0x2d
adder: times 16 db 0x2f
 
section .bss
buf: resb SIZE

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.