我正在尝试编写为字符串分配一些内存并用随机字符填充该内存的代码。 现在我决定采用mmap-syscalls的方式,而不使用malloc函数。
这是我的代码:
.code32
.data
random: .ascii "/dev/random\0"
.section .data
c: .ascii "t" #just a test static character, for test
n: .ascii "\n" #end of string
.text
.global _start
_start:
pushl $0 # offset of 0
pushl $-1 # the file handle of the open file
pushl $33 # MAP_SHARED flag set to write changed data back to file
pushl $3 # PROT_READ and PROT_WRITE permissions
pushl $42
pushl $0 # Allow the system to select the location in memory to start
movl %esp,%ebx # copy the parameters location to EBX
movl $90,%eax # set the system call value
int $0x80
movl %eax,%edi #the adress of allocated memory is stored in edi
pushl %eax #save the adress on stack
movl $42, %ecx #now i want to generate 42 random symbols and print them
loop:
dec %ecx
pushl %ecx #generate a random number, using the kernel Entropy Collector
movl $5,%eax # sys_open
movl $random,%ebx # Filename string
movl $0,%ecx # O_RDONLY flag
int $0x80
# Read one random number
movl %eax,%ebx # The result of sys_open
movl $3,%eax # sys_read
movl (%esp),%ecx # The stack is our buffer
movl $1,%edx #
int $0x80 #random number on stack
popl %eax #eax represents the random number
movl $100,%ebx
divl %ebx #modulo 223
leal 34(%edx),%eax
stosb #load a random char into allocated memory
popl %ecx
cmpl $0,%ecx
jne loop
movl $4,%eax #syscall write
movl $1,%ebx
movl $43,%edx
popl %ecx #pop the saved adress of string
int $0x80
movl $1,%eax #exit
int $0x80
`
导致MOVSB
- 操作中的段错误。
所以我不确定几件事情:
ESI
之前将33到255之间的整数推送到MOVSB
的方式,希望整数被识别为ascii符号,它就是这个符号。LEAL
- 操作的正确性,
它的目的是在每次跳转到一个新字节时移动分配的(42 ??)字节的内存。虽然这个错误导致错误导致MOVSB
。