Created
September 23, 2014 16:54
-
-
Save diogovk/b07eaddafa04f2252dda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; random - programa que imprime uma letra e um digito aleatório em Linux/x86 | |
; para compilar: | |
; nasm -f elf -g -F dwarf -l random.lst random.asm32 | |
; ld -o random random.o | |
; syscalls | |
%define sys_exit 1 | |
%define sys_read 3 | |
%define sys_write 4 | |
%define sys_open 5 | |
; ksyscall(): calls the kernel syscall in eax | |
%macro ksyscall 0 | |
int 0x80 ; chama syscall em linux i386 | |
%endmacro | |
; print(*string, strlen): Will print strlen caracters of string | |
%macro print 2 | |
%define stdout 1 | |
mov ecx, %1 | |
mov edx, %2 | |
mov ebx, stdout | |
mov eax, sys_write | |
ksyscall | |
%endmacro | |
; exit(): Exits returning success | |
%macro exit 0 | |
mov eax, sys_exit | |
mov ebx, 0 ; 0 => shell success | |
ksyscall | |
%endmacro | |
; open_r(*filename): opens file in read mode returning a fd. Returns negative in case of an error | |
%macro open_r 1 | |
%define O_RDONLY 00000000 ; definido em fcntl.h | |
mov ebx, %1 | |
mov eax, sys_open | |
mov ecx, O_RDONLY | |
ksyscall | |
%endmacro | |
; read(fd, *buff, count): reads up to count bytes in buff from open fd | |
%macro read 3 | |
mov ebx, %1 | |
mov ecx, %2 | |
mov edx, %3 | |
mov eax, sys_read | |
ksyscall | |
%endmacro | |
;byte_div(dividend, divisor): divide two bytes. quotient will be in AL and remainder in AH | |
%macro byte_div 2 | |
mov al, %1; [urand_buffer] ; al = urand_buffer[0] | |
mov cl, %2 | |
mov edx, 0 ; para divisao de byte deve-se zerar edx e ah | |
mov ah, 0 | |
div cl | |
%endmacro | |
section .data | |
urand_path db '/dev/urandom',0 ; interface do kernel para leitura de dados pseudo-aleatorios | |
outbuffer db ` \n` ; char[2] usado para imprimir um character + quebra de linha | |
section .bss | |
urand_buffer resw 1 ; define uma word (2 bytes) para para leitura | |
%define urand_buffer_size 2 | |
section .text | |
global _start | |
_start: ;linker entry point | |
open_r urand_path ; abre /dev/random. fd fica em eax | |
read eax, urand_buffer, urand_buffer_size ; le 2 bytes aleatorios em urand_buffer | |
print_random_digit: | |
byte_div [urand_buffer], 10 ; ah = urand_buffer[0] % 10 | |
add ah, 48 ; ah += 48 -> converte numero de 0-9 para o ascii '0'-'9' | |
mov [outbuffer], ah ; outbuffer[0] = ah | |
print outbuffer, 2 ; imprime | |
print_random_letter: | |
byte_div [urand_buffer+1], 26 ; ah = urand_buffer[1] % 26 | |
add ah, 97 ; ah += 97 -> converte numero de 0-25 para o ascii 'a'-'z' | |
mov [outbuffer], ah ; outbuffer[0] = ah | |
print outbuffer, 2 ; imprime | |
end: | |
exit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment