Link to home
Start Free TrialLog in
Avatar of InteractiveMind
InteractiveMindFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Segments

Hey all,

Okay; complete asm noob here.

Tell me all there is to know about "segments" ... please.

:-)
Avatar of Harisha M G
Harisha M G
Flag of India image

Hi, Do you know any processor architecture ?

segments are memory banks, usually of 64 kb

Whenever a program is loaded it is assigned some segments, and the instruction pointer is used to just point to an offset in that segment

---
Harish
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Basically there are six segments.

Code, Data, Extra and Stack segments (available from 8086) (CS, DS, ES, SS)

And another two in 80286+ (FS, GS)

What actually is your problem ?
Avatar of InteractiveMind

ASKER

Yup, this is the right direction, cheers :)

I'm teaching myself asm, and have come across things like "segment .data", and .bss, etc.. and didn't really understand what it all meant.

To be honest, I'd still appreciate some elaboration. I understand mostly what you're saying, but I need to understand things well enough to be able to explain them to someone else, in my own words :)  (well, at least that's the idea...)

Thanks loads.. I can see that this TA is soon to become my new home. :)
Perhaps I should ask more about registers before getting into segments?
No, first you go for 8085 ... then for 8086 and segments
8085 doesn't have any segments. All code is just dumped into memory and run sequentially. However, in 8086 and above, segments are introduced, which make the code relocatable.

Not sure you understand it before knowing 8085 and as you told .. registers.
I see.

With registers, does each one have a special purpose? i.e., should AX be used for something other than BX, etc ?
Furthermore, is there a reason why this code won't compile for me?

;
; first.asm
;

.model small
.stack
.data
message   db "Hello world, I'm learning Assembly !!!", "$"

.code

main   proc
   mov   ax,seg message
   mov   ds,ax

   mov   ah,09
   lea   dx,message
   int   21h

   mov   ax,4c00h
   int   21h
main   endp
end main


I'm on a WinXP here, using NASM (nasmw.exe) for compilation.

Errors:

first.asm:1: error: attempt to define a local label before any non-local labels
first.asm:1: error: parser: instruction expected
first.asm:2: error: attempt to define a local label before any non-local labels
first.asm:3: error: attempt to define a local label before any non-local labels
first.asm:8: error: parser: instruction expected
first.asm:18: error: symbol `main' redefined
first.asm:18: error: parser: instruction expected
first.asm:19: error: parser: instruction expected
Not sure. In MASM it is assembling with 0 errors and 0 warnings.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See whether it assembles:

data_seg segment data
      message   db "Hello world, I'm learning Assembly !!!", "$"
data_seg ends

code_seg segment code
assume cs:code_seg, ds:data_seg
start:
      mov   ax, data_seg
      mov   ds, ax

      mov   ah, 09
      lea   dx, message
      int   21h

      mov   ax, 4c00h
      int   21h
code_seg ends
end start
Err... give quotes around code and data. Also, you can add stack segment.

stack_seg segment stack
      stack_top db 10h
stack_seg ends

data_seg segment 'data'
      message   db "Hello world, I'm learning Assembly !!!", "$"
data_seg ends

code_seg segment 'code'
assume cs:code_seg, ds:data_seg
start:
      mov   ax, data_seg
      mov   ds, ax

      mov   ah, 09
      lea   dx, message
      int   21h

      mov   ax, 4c00h
      int   21h
code_seg ends
end start
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi InteractiveMind,

A good place to start is to write some C, compile it with flags to generate assembler+source listing and look at the result.

Generally, the segments defined in the assembler are suggestions for the linker about how to structure your program in memory. Most segments end up grouped into code segments and data segments. The linker pulls them into their groups and assigns segment registers and offsets for each group.

Paul
What flags would I need to set to generate assembler+source listing? I use Dev-C++ with MinGW32 for my C/C++ ...

Thanks
For DevC++...

Goto

C:\Dev-Cpp\Bin

gcc -S <filepath>

For TurboC

Goto

(Folder may change)
C:\TC\BIN

tcc -S <filepath>

Note that S is in uppercase
In the first case you will get a .S file, while in the second you get a .ASM file
Ah cool.

Harish, do you use MASM32 for assembling? If so, what command do you use for it?

Cheers.
MASM /ZI <filename>;
LINK <filename>;

(If you want to debug)
CV <filename>  
(If you want to run)
<filename>

Hmm; I wasn't haven't a great deal of luck with NASM, so I thought I'd switch to MASM. I downloaded it from www.masm32.com, and installed it. However, I'm unable to find MASM.exe (or anything which seems to blatently be the assembler). Any ideas?
Is that Microsoft's Macro Assembler ? It doesn't seem to be. I use MASM6 not MASM32
InteractiveMind,

If you have DevC++, then you can goto Dev-Cpp\Bin and use

gcc -x assembler <filename>

http://www.doc.ic.ac.uk/lab/labman/lookup-man.cgi?gcc(1)