Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ".intel_syntax noprefix" how can I get memory address of a label?

I'm learning how to create real mode programs assembled and linked with:

  • GCC Assembler version 2.25
  • Binutils version 2.25
  • GCC version 5.2.0

I use Intel syntax without prefixes specified with .intel_syntax noprefix

I want to load the address of a string into the SI register. I know, how to refer to a memory location in NASM, but when I do the same inside the GNU Assembler code, it only moves a WORD of the string into the register instead of the address of the label.

My code is:

.code16
.intel_syntax noprefix

mov cx, 11
mov si, name
mov di, 0x7E00
push di
rep cmpsb
pop di
je LOAD

When I compile it with GCC, I can see in the debugger that the processor does:

mov si, WORD ptr ds:0x7d2d

but I want the address of the string moved into the register, not the data it points at.

I also tried to put square brackets around the name like this:

.code16
.intel_syntax noprefix

mov cx, 11
mov si, [name]
mov di, 0x7E00
push di
rep cmpsb
pop di
je LOAD

It doesn't make a difference.

From NASM I know that this assembly code works:

mov si, name

NASM will generate the instruction that moves the address of name into register SI.

My question: Is there a way to force GCC using Intel Syntax with No Prefix to load the address of a symbol into a register?

like image 830
TheDome Avatar asked Apr 27 '16 19:04

TheDome


1 Answers

If you are using GNU assembler code (via GCC) with the directive:

.intel_syntax noprefix

Then the syntax can seem a bit unusual, and not well documented in canonical sources. This code:

mov si, name

Moves the 16-bit WORD at memory location pointed to by label name into SI. The following instructions do the same thing, although I prefer the square brackets [ and ] because the code's intent is clearer:

mov si, name              
mov si, [name]
mov si, word ptr [name]   # This is the same as two above in long form
                          #     The use of "word ptr" here is similar to MASM

To get the address of name and place it into the register you need to use offset like this:

mov si, offset name

If someone has a canonical source that actually describes the syntax that can be used with .intel_syntax noprefix please let me know in the comments. I am aware of blogs about the syntax, along with some contempt, but nothing official.

like image 91
Michael Petch Avatar answered Oct 23 '22 08:10

Michael Petch