我在这里找到了一些关于将值转换为 ascii 的主题。但我有点卡在这里。这是我的代码:
;GetPID
SECTION .data
msg: db "Your PID is:" ;pidmsg
msgl: equ $-msg ;pidlen
lookup: db "0123456789"
SECTION .bss
pid: resb 8 ;test is a variable im testing to
;receive the eax from sys_getpid
SECTION .text
global _start
_start:
call getpid ;get pid number into eax
mov ebx,0xa
lea ebp, [pid+6]
call ASCIIC
jmp exit ;exit
exit:
mov eax,1
mov ebx,0
int 0x80
getpid:
mov eax,20 ;getpid function (sys_getpid)
int 0x80
ret
;ASCIIC was taken from http://theropfather.github.io/asm/getpid_tutorial.html
ASCIIC:
div ebx ;Divide the PID
mov byte cl, [lookup+edx] ;Copy ASCII value to CL
mov [ebp], cl ;Copy ASCII value to buffer
dec ebp ;Next byte into buffer
xor edx, edx ;Clear the remainder
inc eax ;Dec eax tricking jnz
dec eax ;Push back to original value
jnz ASCIIC ;Keep looping until eax is zero
call .printPID ;Print out the buffer
ret
.printPID:
mov ecx, msg ;message
mov edx, msgl ;msg len
mov ebx,0x1 ;FD stdout
mov eax, 0x4 ;sys_write call
int 0x80 ;Call
mov [pid+7], byte 0xA ;Push a newline to PID string
mov edx,0x8 ;Max length of 8 bytes
mov ecx,pid ;Push PID value
mov ebx,0x1 ;FD stdout
mov eax, 0x4 ;sys_write call
ret
所以这里的重点是获取当前程序(this)的pid号并打印出来。程序调用 sys_getpid (eax=20) 并返回 pidnumber。但是,sys_write 参数是:ssize_t sys_write(unsigned int fd, const char * buf, size_t count)
第二个参数是一个字符,所以我需要为每个数字转换成字符。
输出仅为:“您的 PID 是:”
任何人 ?:)