Author Topic: how to access data defined without label...  (Read 20239 times)

Offline nasmkid

  • Jr. Member
  • *
  • Posts: 3
how to access data defined without label...
« on: November 21, 2010, 06:18:00 PM »
Hi all,

I am defining data section without lables.

Code: [Select]
SECTION .data
         db 0xAB
         db 0xCD
         db 0xEF

SECTION .text
         mov ax, 0xAB
         mov ax, 0xCD
         mov ax, 0xEF

In the above code I want to access data "0xAB 0xCD 0xEF" from "data segment" in code segment. How to do it?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: how to access data defined without label...
« Reply #1 on: November 21, 2010, 06:55:46 PM »
Put labels on 'em! Have you got some reason to not do so?

Alternatively, you could do it DEBUG-style, and just hard-code the offsets, but these will change every time you alter the code. This will soon put you in the nuthouse - visiting hours are Thursdays and Saturdays. :)

What are you trying to accomplish here, Nasmkid?

Best,
Frank


Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: how to access data defined without label...
« Reply #2 on: November 21, 2010, 07:55:40 PM »
You need some way to know where your data is. Sometimes (such as in the case of data structures that you don't define) these can be hardcoded; most other times, you'll at least want some kind of label as Frank said. (In fact, even defining a structure (struc/res*/endstruc) in NASM is pretty much using hardcoded offsets with fancy names.) However, it is often easier and more effective not to define and use a label for each data item that you define. If you are familiar with high-level languages such as C, an array (or put simply: a table of multiple items) is actually defined with a single "label", which shows you where the first item of that array (= table) is. The address of later items is implicitly computed, for example, by adding the size of a single item to the current address. (This is the way C implementations access arrays.)

Off-topic: I just have to add that misusing a debugger as assembler for any actual development is a painful and, today, useless practice. Most debuggers are only intended for debugging and testing out stuff and that's what they do best. For example, I still use (FreeDOS) DEBUG to test out small code sequences if I'm unsure of how exactly an instruction works. Anything longer than 5 lines goes into a NASM source code file!
C. Masloch

Offline nasmkid

  • Jr. Member
  • *
  • Posts: 3
Re: how to access data defined without label...
« Reply #3 on: November 26, 2010, 11:46:26 AM »
Hi guys,

I have simply confused, because of the NASM manual (may be my mistake), In the manual, they given some example for "db", "dw", etc.,

In that, they just specified,

  db 0xAB
  dw 0xABCD

without label.

So, I encountered some research of, where to use & how to use these data definitions without label.

One usage I have found is, to define 0xAA55 at 510th byte for boot loader. Anything else ?

Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: how to access data defined without label...
« Reply #4 on: November 26, 2010, 06:27:32 PM »
One usage I have found is, to define 0xAA55 at 510th byte for boot loader. Anything else ?

It's the 511th (and following 512th) byte of the boot sector, which is at the address 510. (Addresses are zero-based, while a natural language ordinal is one-based.)

Like other usages of label-less data or code, the address is implicitly assumed to be 510. With the boot loader, that assumption just happens to be in the BIOS: your code doesn't have to access the signature.
C. Masloch

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: how to access data defined without label...
« Reply #5 on: November 26, 2010, 07:14:22 PM »
Good example, Nasmkid! I suppose the 'M', 'Z' at the start of a dos file, or the 7Fh, 'E', 'L', 'F' at the start of a Linux file would be examples. You don't "need" a label. As Christian points out, we don't always need to access these bytes from our code. S'pose we did:

Code: [Select]
cmp word [0x7DFE], 0xAA55 ; assume ds=0
jne invalid  ; could use a hard coded number here, too!

Or, we could put a label on it, and do:

Code: [Select]
cmp word [bootsig], 0xAA55
jne invalid

The only "cost" involved is the hardship of typing in the labels. And it makes Nasm do more work, but that's its job!

Essentially, all of assembly language is "fancy names for some numbers". Might as well try to make the names "meaningful". But it's a "convenience" for the programmer, not a requirement.

Best,
Frank