gpt4 book ai didi

linux - 显示寄存器内容

转载 作者:可可西里 更新时间:2023-11-01 11:48:50 26 4
gpt4 key购买 nike

您好,我需要显示寄存器内容的帮助。我的代码如下。我已经能够显示数据寄存器的值,但我想显示标志状态。例如 1 或 0。如果还显示其他寄存器(如 esi、ebp)的内容将很有帮助。

我的代码没有打印标志的状态..我错过了什么

section .text
global _start ;must be declared for using gcc
_start : ;tell linker entry point

mov eax,msg ; moves message "rubi" to eax register
mov [reg],eax ; moves message from eax to reg variable


mov edx, 8 ;message length
mov ecx, [reg];message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel

mov eax, 100
mov ebx, 100
cmp ebx,eax

pushf
pop dword eax

mov [save_flags],eax

mov edx, 8 ;message length
mov ecx,[save_flags] ;message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80


mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel

section .data

msg db "rubi",10

section .bss

reg resb 100

save_flags resw 100

最佳答案

我不会在这里做任何花哨的事情,因为这似乎是一项家庭作业(今天有两个人问了同样的问题)。这段代码应该做成一个函数,这样可以提高它的性能。由于我没有获得荣誉学位或类(class) A,因此提供最佳解决方案对我来说没有意义,但您可以从以下方面着手:

    BITS_TO_DISPLAY equ 32       ; Number of least significant bits to display (1-32)

section .text
global _start ; must be declared for using gcc
_start : ; tell linker entry point

mov edx, msg_len ; message length
mov ecx, msg ; message to write
mov ebx, 1 ; file descriptor (stdout)
mov eax, 4 ; system call number (sys_write)
int 0x80 ; call kernel

mov eax, 100
mov ebx, 100
cmp ebx,eax

pushf
pop dword eax

; Convert binary to string by shifting the right most bit off EAX into
; the carry flag (CF) and convert the bit into a '0' or '1' and place
; in the save_flags buffer in reverse order. Nul terminate the string
; in the event you ever wish to use printf to print it

mov ecx, BITS_TO_DISPLAY ; Number of bits of EAX register to display
mov byte [save_flags+ecx], 0 ; Nul terminate binary string in case we use printf
bin2ascii:
xor bl, bl ; BL = 0
shr eax, 1 ; Shift right most bit into carry flag
adc bl, '0' ; bl = bl + '0' + Carry Flag
mov [save_flags-1+ecx], bl ; Place '0'/'1' into string buffer in reverse order
dec ecx
jnz bin2ascii ; Loop until all bits processed

mov edx, BITS_TO_DISPLAY ; message length
mov ecx, save_flags ; address of binary string to write
mov ebx, 1 ; file descriptor (stdout)
mov eax, 4 ; system call number (sys_write)
int 0x80

mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel

section .data
msg db "rubi",10
msg_len equ $ - msg

section .bss
save_flags resb BITS_TO_DISPLAY+1 ; Add one byte for nul terminator in case we use printf

此代码背后的想法是,我们不断地将 EAX 寄存器中的位(使用 SHR instruction)一次向右移动一位。移出寄存器的位被放置在进位标志 (CF) 中。我们可以使用 ADC将进位标志 (0/1) 的值添加到 ASCII“0”以获得“0”和“1”的 ASCII 值。我们以相反的顺序将这些字节放入目标缓冲区,因为我们是从右向左移动这些位。

BITS_TO_DISPLAY 可以设置在 1 到 32 之间(因为这是 32 位代码)。如果您对寄存器的低 8 位感兴趣,请将其设置为 8。如果您想显示 32 位寄存器的所有位,请指定 32。

关于linux - 显示寄存器内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com