Assembly 如何用汇编语言(英特尔)执行机器指令
例如,如果我有0000101这样的指令,并且我在ram中有它供程序访问,那么我如何能够在不使用OS函数的情况下在汇编语言中执行该指令?我正在为英特尔使用Fasm。谢谢 编辑:我知道这是非常糟糕的代码,我甚至还没有组装它,我知道很多是错误的,但请记住这是为了学习的目的。这是加载带有二进制指令的文件并将其存储在ram中的代码部分。我又一次知道这很糟糕Assembly 如何用汇编语言(英特尔)执行机器指令,assembly,x86,fasm,Assembly,X86,Fasm,例如,如果我有0000101这样的指令,并且我在ram中有它供程序访问,那么我如何能够在不使用OS函数的情况下在汇编语言中执行该指令?我正在为英特尔使用Fasm。谢谢 编辑:我知道这是非常糟糕的代码,我甚至还没有组装它,我知道很多是错误的,但请记住这是为了学习的目的。这是加载带有二进制指令的文件并将其存储在ram中的代码部分。我又一次知道这很糟糕 loadkernel: mov dx, 1F7h in dx, bl bt bl, 6 ;this reads the
loadkernel:
mov dx, 1F7h
in dx, bl
bt bl, 6 ;this reads the sixth bit of bl and stores it in the carry flag(cf)
cmp cf, 1 ;if bit 6 is one, then the hard drive is signaling that it is ready for the next operation
jz loadkernel
clc ;clear carry flag
beginload:
mov eax, 300h
mov ecx, eax ;copy the starting point of the kernel in memory to ecx
mov ebx, 0 ;clear
mov edx, 0 ;clear
mov bl, 1F4h
out ebx, bl ;give the hard drive the low address of the location of the kernel
mov bl, 1F5h
out 0h, bl ;give the hard drive the high address of the location of the kernel
mov bl, 1F0h
in edx, bl ;read the hard drive
mov [eax], edx ;add kernel data to memory
add eax, 1
inc ebx ;move the hard drive reading head thing forward
mov ip, [eax] ;mov the instruction pointer to memory, so that the computer excecutes the kernel
cmp edx, 0AA55h
jz beginload ;if 0AA55h is not at the end, then read the next data of the kernel.
只需跳转到存储指令的地址。只需跳转到存储指令的地址。根据您的执行环境,您可能必须为您的程序禁用(大多数)操作系统的执行禁用安全性。这是为了使易受攻击的程序更难注入代码。如果您在一个独立的环境(如DOS或您自己的内核)中运行,则无需担心这一点 无论如何,你所要做的就是:
mov ax,0x9090 //0x90 is opcode for NOP
mov [code],ax
code:
jmp foo //this is a 2-byte opcode (so long as it does the "correct" behavior and generate a relative jmp
bar:
hlt //this will get executed "magically"
foo:
//won't get here
根据您的执行环境,您可能必须为您的程序禁用(大多数)操作系统的执行禁用安全性。这是为了使易受攻击的程序更难注入代码。如果您在一个独立的环境(如DOS或您自己的内核)中运行,则无需担心这一点 无论如何,你所要做的就是:
mov ax,0x9090 //0x90 is opcode for NOP
mov [code],ax
code:
jmp foo //this is a 2-byte opcode (so long as it does the "correct" behavior and generate a relative jmp
bar:
hlt //this will get executed "magically"
foo:
//won't get here
也许您可以提供一些代码;-)是的,甚至是地址。你必须计算它,这取决于你从哪里跳。当然,地址必须在.code部分,而不是.data部分。在操作系统上不起作用,一旦你破坏了所有的保护,那么是的,就是这么简单,把指令写在某个地方,然后简单地分支到它们。这是一个自我修改的代码问题的副本,也许您可以提供一些代码;-)是的,甚至是地址。你必须计算它,这取决于你从哪里跳。当然,地址必须在.code部分,而不是.data部分。在操作系统上不起作用,一旦你破坏了所有的保护,那么是的,就是这么简单,把指令写在某个地方,然后简单地分支到它们。这是一个非常有用的自修改代码问题的副本,这是最快/唯一的方法?@kjmcgrinder当然,您希望一次复制所有代码并一次执行所有代码,但这取决于您想要执行的操作。还要注意的是,加载这样的代码会使缓存等失效,并使其性能更差,因此请将代码写入内存一次,如果不需要,请不要再写入。这是非常有用的,这是最快/唯一的方法?@kjmcgrinder当然,您希望一次复制所有代码并一次执行,但是的,取决于你想做什么。还要注意,加载这样的代码会使缓存等失效,使其性能更差,因此请将代码写入内存一次,如果没有必要,请不要再次写入