Here is how it looks like in WASM assembly:
Code: Select all
UserGate Macro gate_nr
db 67h
db 9Ah
dd gate_nr
dw 3
Endm
However, GCC cannot inline this without duplicating the code!
The obvious way to do it:
Code: Select all
#define UserGate(nr) \
asm ( \
".byte 0x67\n\t"
".byte 0x9A\n\t"
".int %0\n\t"
".word 0x3\n\t"
: \
: "i" (nr) \
);
int main()
{
UserGate(0x123);
return 0;
}
This isn't a big problem, since it is the 32-bit version, and I might just as well stick to OpenWatcom for that.
For a 64-bit version it look like this:
Code: Select all
#define UserGate(nr) \
asm ( \
"pushq %%r15\n\t"
"movq %0, %%r15\n\t"
"syscall\n\t"
"popq %%r15\n\t"
: \
: "i" (nr) \
);
int main()
{
UserGate(0x123);
return 0;
}
Code: Select all
static inline int MyAsm()
{
int res;
UserGate(0x123);
asm("movl %0, %%eax" : "=r" : );
return res;
}
int main()
{
int rand = MyAsm();
return 0;
}
Code: Select all
00000180e000021c <MyAsm>:
180e000021c: 55 push %rbp
180e000021d: 48 89 e5 mov %rsp,%rbp
180e0000220: 41 57 push %r15
180e0000222: 49 c7 c7 14 03 00 00 mov $0x314,%r15
180e0000229: 0f 05 syscall
180e000022b: 41 5f pop %r15
180e000022d: 89 c0 mov %eax,%eax
180e000022f: 89 45 fc mov %eax,-0x4(%rbp)
180e0000232: 8b 45 fc mov -0x4(%rbp),%eax
180e0000235: 5d pop %rbp
180e0000236: c3 retq
00000180e0000237 <main>:
180e0000237: 55 push %rbp
180e0000238: 48 89 e5 mov %rsp,%rbp
180e000023b: 48 83 ec 10 sub $0x10,%rsp
180e000023f: b8 00 00 00 00 mov $0x0,%eax
180e0000244: e8 d3 ff ff ff callq 180e000021c <MyAsm>
180e0000249: 89 45 fc mov %eax,-0x4(%rbp)
180e000024c: b8 00 00 00 00 mov $0x0,%eax
180e0000251: c9 leaveq
180e0000252: c3 retq
Edit: Further reading gives a solution:
Code: Select all
inline int MyAsm() __attribute__ ((always_inline));
inline int MyAsm()
{
int res;
UserGate(0x123);
asm("movl %0, %%eax" : "=r" : );
return res;
}
int main()
{
int rand = MyAsm();
return 0;
}
Code: Select all
00000180e0000237 <main>:
180e0000237: 55 push %rbp
180e0000238: 48 89 e5 mov %rsp,%rbp
180e000023b: 41 57 push %r15
180e000023d: 49 c7 c7 14 03 00 00 mov $0x314,%r15
180e0000244: 0f 05 syscall
180e0000246: 41 5f pop %r15
180e0000248: 89 c0 mov %eax,%eax
180e000024a: 89 45 f8 mov %eax,-0x8(%rbp)
180e000024d: 8b 45 f8 mov -0x8(%rbp),%eax
180e0000250: 89 45 fc mov %eax,-0x4(%rbp)
180e0000253: b8 00 00 00 00 mov $0x0,%eax
180e0000258: 5d pop %rbp
180e0000259: c3 retq