gas

x86 assembler: floating point compare

旧街凉风 提交于 2019-11-26 18:59:00
As part of a compiler project I have to write GNU assembler code for x86 to compare floating point values. I have tried to find resources on how to do this online and from what I understand it works like this: Assuming the two values I want to compare are the only values on the floating point stack, then the fcomi instruction will compare the values and set the CPU-flags so that the je , jne , jl , ... instructions can be used. I'm asking because this only works sometimes. For example: .section .data msg: .ascii "Hallo\n\0" f1: .float 10.0 f2: .float 9.0 .globl main .type main, @function main:

GCC: Prohibit use of some registers

十年热恋 提交于 2019-11-26 17:46:15
问题 This is a strange request but I have a feeling that it could be possible. What I would like is to insert some pragmas or directives into areas of my code (written in C) so that GCC's register allocator will not use them. I understand that I can do something like this, which might set aside this register for this variable register int var1 asm ("EBX") = 1984; register int var2 asm ("r9") = 101; The problem is that I'm inserting new instructions (for a hardware simulator) directly and GCC and

x86 Linux assembler get program parameters from _start

馋奶兔 提交于 2019-11-26 16:56:15
问题 I'm trying to create a program to just write the param on the screen. I created some programs to get the C function parameter, or i used C to send the parameter to my asm program. Is there a way to get the program parameter using only assembler EX: ./Program "text" I'm using as (Gnu Assembler) Usually i get those parameters using [esp+4] Because the esp is the program/function call pointer, but in pure asm it don't get the command line parameter. Is there a way to do that? I googled it, but i

How to produce a minimal BIOS hello world boot sector with GCC that works from a USB stick on real hardware?

老子叫甜甜 提交于 2019-11-26 16:54:09
问题 I have managed to produce a minimal boot sector that works with QEMU 2.0.0 Ubuntu 14.04: .code16 .global _start _start: cli mov $msg, %si mov $0x0e, %ah loop: lodsb or %al, %al jz halt int $0x10 jmp loop halt: hlt msg: .asciz "hello world" .org 510 .word 0xaa55 Compiled with: as -o main.o main.S ld --oformat binary -o main.img -Ttext 0x7C00 main.o The example is available on this repo: https://github.com/cirosantilli/x86-bare-metal-examples/tree/2b79ac21df801fbf4619d009411be6b9cd10e6e0/no-ld

How to get the size of a C function from inside a C program or with inline assembly?

可紊 提交于 2019-11-26 16:46:23
问题 Suppose I have a function like below: # cat 003.c int foo(int a, int b) { return a+b; } And compile it like this: gcc -S 003.c The gets the following assembly result: .file "003.c" .text .globl foo .type foo, @function foo: .LFB2: pushq %rbp .LCFI0: movq %rsp, %rbp .LCFI1: movl %edi, -4(%rbp) movl %esi, -8(%rbp) movl -8(%rbp), %edx movl -4(%rbp), %eax addl %edx, %eax leave ret .LFE2: .size foo, .-foo /* size of the function foo, how to get it?*/ The last line above do get the size of the

What does the bracket in `movl (%eax), %eax` mean?

允我心安 提交于 2019-11-26 14:17:05
问题 I have googled enough but could not figure out what the bracket () means. Also, I see some syntax as movl 8(%ebp), %eax Could some someone suggest me some good reference? I have not been able to find any in the top 20 results from Google. 回答1: %eax is register EAX; (%eax) is the memory location whose address is contained in the register EAX; 8(%eax) is the memory location whose address is the value of EAX plus 8. 回答2: http://web.archive.org/web/20080215230650/http://sig9.com/articles/att

Calling printf in x86_64 using GNU assembler

泪湿孤枕 提交于 2019-11-26 11:33:15
问题 I\'ve written a program using AT&T syntax for use with GNU assembler: .data format: .ascii \"%d\\n\" .text .global main main: mov $format, %rbx mov (%rbx), %rdi mov $1, %rsi call printf ret I use GCC to assemble and link with: gcc -o main main.s I run it with this command: ./main When I run the program I get a seg fault. By using gdb, it says printf not found. I have tried \".extern printf\", which does not work. Someone suggested I should store the stack pointer before calling printf and

How to generate plain binaries like nasm -f bin with the GNU GAS assembler?

﹥>﹥吖頭↗ 提交于 2019-11-26 07:38:37
问题 I have some NASM files that generally have the structure: [BITS 64] [ORG 0x0000000000200000] start: ... ret I\'m assembling them like so: nasm -f bin abc.asm I\'d like to write some of these using GAS instead. Two questions: What directives should I use in GAS? I\'ve found the \'.org\' directive but GAS doesn\'t seem to have a \'.bits\' directive. What should I pass to gcc or as to generate a plain binary file? I.e. what the -f bin option does with NASM. 回答1: What directives should I use in

What does cltq do in assembly?

烂漫一生 提交于 2019-11-26 06:47:55
问题 0x0000000000400553 <main+59>: mov -0x4(%rbp),%eax 0x0000000000400556 <main+62>: cltq 0x0000000000400558 <main+64>: shl $0x3,%rax 0x000000000040055c <main+68>: mov %rax,%rdx In fact my programe is as simple as : 5 int main(int argc, char *argv[]) { 6 int i = 0; 7 while(environ[i]) { 8 printf(\"%s\\n\", environ[i++]); 9 } 10 return 0; But the assembly output is pretty long: Dump of assembler code for function main: 0x0000000000400518 <main+0>: push %rbp 0x0000000000400519 <main+1>: mov %rsp,