Jump to content

[x86 ASM] alpha-numeric keys

- - - - -

  • Please log in to reply
No replies to this topic

#1
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
Using C std functions and the not so random rand. Although the code does demonstrate using lookup tables in assembly so I figure I would post it.

[SECTION .text]			; Section containing code


extern _rand

extern _srand

extern _time

extern _printf

global _main			; Required so linker can find entry point

	

_main:

	enter 0,0

    

	push ebp		; Set up stack frame for debugger

	mov ebp,esp

	push ebx		; Program must preserve ebp, ebx, esi, & edi

	push esi

	push edi

	;;; Everything before this is boilerplate; use it for all ordinary apps!	

	mov ebx, 31			;max we want is 32 digits, this is i

	xor edi, edi		;this will be our dest index to our string

	call seedit			;seed the random number generator


.loop:					;the loop to fill our string - local label

	dec ebx				;take our max counter down 1 - i--

	call pull6			;pull 6 digits out of a binary string

	inc edi				;increment edi by 1 to follow through the string of randoms

	cmp ebx, 0			;are we done yet?

	jnz .loop			;nope? ok loop back and do it again

	

	push dword randnum	;push the address of the string of randoms

	push dword opbuff	;push the address of the display buffer

	call _printf		;call c function printf

	add esp, 8			;clean up stack

	

	mov eax, 1

	call printnl

	

	;;; Everything after this is boilerplate; use it for all ordinary apps!

	pop edi			; Restore saved registers

	pop esi

	pop ebx

	mov esp,ebp		; Destroy stack frame before returning

	pop ebp


	leave

	ret			; Return control to Linux


seedit:

	push dword 0		;time wants a null pushed

	call _time			;call time

	add esp, 4			;clean up stack

	push eax			;second count returned in eax so push it as variable to srand

	call _srand			;call srand with our time seed

	add esp, 4			;clean up stack

	ret

	

pull6:					;pulls 6 digits out of the random numbers binary string

	mov ecx, 25			;load ecx with how many to shift off

	jmp pull			;jump over pull4

pull4:					;pulls 4 digits

	mov ecx, 27			;load ecx with how many to shift off

pull:					;does the real work!

	push ecx			;rand will destroy our ecx so save it on the stack

	call _rand			;call rand

	pop ecx				;pop our ecx off the stack

	shr eax, cl			;shift off by how many we loaded earlier

	cmp eax, 35d		;compare the shifted value to 35 decimal

	jnle pull6			;eax <= 35 ??? if not pull a new number

	mov cl, [chartbl + eax]	;pull the num/char equiv from our char table

	mov [randnum+edi], cl	;load the pulled num/char into our string via offset edi

	ret						;go home

	

;Pass number of newlines to print in eax

;0 does nothing

;1-10 prints the number of nl chars specified

printnl:

	mov ecx, 10			;10 is max number of newline chars

	sub ecx, eax		;so sub how many the user wants from max and we know how far to skip into nl

	add ecx, nl			;add the skip amount into the address of nl

	push ecx			;push the address

	call _printf		;call printf to print em out

	add esp, 4			;clean up stack

	ret					;go home

nl db 10,10,10,10,10,10,10,10,10,10,0	;10 newlines and a null at the end



[SECTION .data]			; Section containing initialised data

		

opbuff	dw 'Random value is %s', 10, 0

chartbl db '0123456789ABCDEF0123456789ABCDEF0123'


[SECTION .bss]			; Section containing uninitialized data

randnum resb 36





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users