Using Intel syntax in GAS causes OS to significantly slower

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
PhantomWhiskers
Posts: 7
Joined: Wed Sep 21, 2016 1:54 am

Using Intel syntax in GAS causes OS to significantly slower

Post 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?
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Using Intel syntax in GAS causes OS to significantly slo

Post 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?
PhantomWhiskers
Posts: 7
Joined: Wed Sep 21, 2016 1:54 am

Re: Using Intel syntax in GAS causes OS to significantly slo

Post 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?
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Using Intel syntax in GAS causes OS to significantly slo

Post 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.
PhantomWhiskers
Posts: 7
Joined: Wed Sep 21, 2016 1:54 am

Re: Using Intel syntax in GAS causes OS to significantly slo

Post 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.
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Using Intel syntax in GAS causes OS to significantly slo

Post 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
PhantomWhiskers
Posts: 7
Joined: Wed Sep 21, 2016 1:54 am

Re: Using Intel syntax in GAS causes OS to significantly slo

Post 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.
Post Reply