嗨,我需要帮助显示寄存器的内容.我的代码在下面.我已经能够显示数据寄存器的值,但我想显示标志状态.例如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
解决方法:
我不会在这里寻找任何有趣的东西,因为这似乎是一项家庭作业(今天有两个人问了同样的问题).此代码应作为一个函数,并可以提高其性能.由于我没有在课堂上获得荣誉学位或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”以获得ASCII值“0”和“1”.我们将这些字节以相反的顺序放入目标缓冲区,因为我们通过位从右向左移动.
BITS_TO_DISPLAY可以设置在1到32之间(因为这是32位代码).如果您对寄存器的低8位感兴趣,请将其设置为8.如果要显示32位寄存器的所有位,请指定32.