user1327203
user1327203

Reputation:

How to excecute machine instructions in assembly language (intel)

If I had an instruction like 00010101 for example, and I had it in ram for programs to access, how would I be able to excecute that instruction in assembly language without using OS functions? I am using Fasm for intel. Thanks.

EDIT: I know this is really crappy code, I havnt even assembled it yet and I know a lot is wrong, but keep in mind this is for learning purposes. This is the part of the code that loads a file with binary instructions and stores it in ram. Once again I know it is very crappy.

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.

Upvotes: 0

Views: 252

Answers (2)

Earlz
Earlz

Reputation: 63835

Depending on your execution environment, you may have to disable (most) OS's Execute-Disable security for your program. This is put into place so that a vulnerable program is much harder to inject code into. If you're running in a freestanding environment such as DOS or your own kernel, this isn't anything to worry about.

Anyway, all you have to do is this:

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

Upvotes: 2

akluth
akluth

Reputation: 8583

Simply jump to the address where the instruction is stored.

Upvotes: 1

Related Questions