Page 1 of 1
Using Intel syntax in GAS causes OS to significantly slower
Posted: Wed Oct 12, 2016 3:02 am
by PhantomWhiskers
I was experimenting with the boot.s program from the Bare Bones tutorial, and I noticed that putting ".intel_syntax noprefix" at the top and changing the mov statement from "mov $stack_top, %esp" to "mov esp, stack_top" causes the OS to run much slower in QEMU.
I wrote this dumb delay function in kernel.c just to test that terminal scrolling was working properly after calling numerous terminal_writestring() functions with new lines:
Code: Select all
void delay(size_t n) {
for (volatile size_t i = 0; i < n; i++) {
for (volatile size_t j = 0; j < 5000000; j++)
;
}
}
When I call "delay(1);" when using intel syntax in the function takes longer than a whole second before the next terminal_writestring gets called, but if I revert boot.s back to ATT syntax, it takes about a tenth of a second for each "delay(1);".
I think I will stick with ATT syntax and just try to get used to it, but does anyone know why Intel syntax with GAS slows everything down by an order of magnitude?
Re: Using Intel syntax in GAS causes OS to significantly slo
Posted: Wed Oct 12, 2016 3:10 am
by iansjack
I can't believe that a mere change in syntax can affect the OS in the way you describe. Whatever the syntax, the same code should be generated (assuming that you have made no errors).
Have you looked at the generated code to see what differences there are?
Re: Using Intel syntax in GAS causes OS to significantly slo
Posted: Wed Oct 12, 2016 3:14 am
by PhantomWhiskers
There are no errors or warnings from compilation, I didn't make any changes at all to the .c or .ld files, and I am not giving the assembler, compiler, and linker any different arguments.
The compilation process is generating .o files (and then removing them after creating an iso, but I could stop it from removing the .o files), how would I go about checking the generated code to see if anything is different?
Re: Using Intel syntax in GAS causes OS to significantly slo
Posted: Wed Oct 12, 2016 3:18 am
by iansjack
PhantomWhiskers wrote:how would I go about checking the generated code to see if anything is different?
Use "objdump" with the "-d" switch.
Simple logic tells you that the syntax can't affect the speed of the code; the machine code has no way of knowing what syntax was used in the source. But if you are generating different code, because you are not using the source to express identical instructions, anything can happen.
Re: Using Intel syntax in GAS causes OS to significantly slo
Posted: Wed Oct 12, 2016 3:39 am
by PhantomWhiskers
I ran objdump -d from both ATT and Intel syntax .o files and here are the outputs:
Code: Select all
boot_att.o: file format elf32-i386
Disassembly of section .text:
00000000 <_start>:
0: bc 00 40 00 00 mov $0x4000,%esp
5: e8 fc ff ff ff call 6 <_start+0x6>
a: fa cli
b: f4 hlt
c: eb fd jmp b <_start+0xb>
Code: Select all
boot_intel.o: file format elf32-i386
Disassembly of section .text:
00000000 <_start>:
0: 8b 25 00 40 00 00 mov 0x4000,%esp
6: e8 fc ff ff ff call 7 <_start+0x7>
b: fa cli
c: f4 hlt
d: eb fd jmp c <_start+0xc>
It seems that in ATT syntax, the first line shows up as "mov $0x4000,%esp" whereas Intel syntax generates "mov 0x4000,%esp". That has to be the problem, but I still am not understanding why it is causing that behavior. It seems (at least to my understanding, please correct me if I am wrong) that under Intel syntax it isn't giving the stack pointer the correct location for the stack. This confuses me even further because the C code is still able to function (albeit much slower) even when the stack pointer isn't actually pointing to the stack defined in boot.s.
Re: Using Intel syntax in GAS causes OS to significantly slo
Posted: Wed Oct 12, 2016 4:16 am
by Octocontrabass
You're probably expecting GAS Intel syntax to be similar to NASM. Unfortunately, GAS Intel syntax is more like MASM. You need the "offset" keyword in order to get the correct result.
Code: Select all
NASM: mov esp, [stack_top]
GAS: mov esp, stack_top
NASM: mov esp, stack_top
GAS: mov esp, offset stack_top
Re: Using Intel syntax in GAS causes OS to significantly slo
Posted: Wed Oct 12, 2016 4:31 am
by PhantomWhiskers
Octocontrabass wrote:You're probably expecting GAS Intel syntax to be similar to NASM. Unfortunately, GAS Intel syntax is more like MASM. You need the "offset" keyword in order to get the correct result.
Oh wow, thanks! That completely fixed the issue and now there is no difference in performance between Intel and ATT syntax. I figured that was where the issue was, but I wasn't sure how to fix it.
Edit: I guess I should also ask if there are any resources that explain GAS Intel syntax in depth? When I Google it, all I can find are pages that explain how GAS uses ATT syntax, or stack overflow questions asking how to use Intel syntax with GAS.