N00B Question: Let's talk about segment...

Started by parker, March 16, 2012, 07:58:15 AM

Previous topic - Next topic

parker

sir i'm a newbie student and im trying to sort this thing, please enligthen me, i'm confuse.

segment distribution:
        seg1st db 'the quick ' ; place this one 0101h
        seg2nd db 'brown fox ' ; place this one 0102h
        seg3rd db 'jumps over' ; place this one 0103h
     
        how to:
            extract the string 'quick' on first segment
            extract the string 'fox' on second segment
            extract the string 'over' on third segment
            and place the extracted string on 4th segment 0104h with an offset of 10h

       org 100h
               
        mov ax,0101h ;1st segment
        mov ds,ax
        mov ax,0102h ;2nd segment
        mov es,ax
        mov ax,0103h ;3rd segment where should i place this one?
        mov ??,ax    ;ds and es is already occupied.!

I admire the effort of everyone who tend to share their wisdom without any kind in return. Thank you guyz...!

Rockphorr

you can define segment by segment directive

.com program has tiny model  and single segment
program with few segment(dos exe) not requires org directive




a1 segment
str1 db 'sdfd'
a1 ends


.code
mov ax, seg(a1)
mov ds,ax
lea si,str1

; now ds:si is seg:ofs pointer to str1 var.
Strike while the iron is hot - Бей утюгом, пока он горячий

dedndave

you should not assign "hard" values to the segments (101h,102h, etc)
instead, assing names and let the operating system assign the segment addresses
give me some time to swallow a couple cups of coffee and i'll see what i can come up with   :P

P1

#3
parker,

Welcome to MASM32 Forum !!!

Live Long and Prosper !!!

Recommended reading are the forum rules.

And Enjoy !!!
Quote from: parker on March 16, 2012, 07:58:15 AM
sir i'm a newbie student and im trying to sort this thing, please enligthen me, i'm confuse.

segment distribution:
        seg1st db 'the quick ' ; place this one 0101h
        seg2nd db 'brown fox ' ; place this one 0102h
        seg3rd db 'jumps over' ; place this one 0103h
     
        how to:
            extract the string 'quick' on first segment
            extract the string 'fox' on second segment
            extract the string 'over' on third segment
            and place the extracted string on 4th segment 0104h with an offset of 10h

       org 100h
               
        mov ax,0101h ;1st segment
        mov ds,ax
        mov ax,0102h ;2nd segment
        mov es,ax
        mov ax,0103h ;3rd segment where should i place this one?
        mov ??,ax    ;ds and es is already occupied.!


Quote from: Forum Rules9. The NO HOMEWORK Rule.
This is not practical code for programming an application, so it must be an assignment.

You need to invest yourself into the class in order to come away with the experience from it you need.

If you do this right, the experience will enrich your ability to solve the harder issues later.   Please do not 'cheat' yourself now.

Regards,  P1   :8)

dedndave

yah - well, the instructor probably has no idea how to write the code - lol

there are several ways to do this
you can use seperate segments or you can combine them into a GROUP
attached is an example of both ways

you could also use the .MODEL, .STACK, .DATA, .CODE "shortcut" directives
i chose to do it the old way to better illustrate how segments and groups are created

FARMER-OAK

Hi Parker
Understanding segments is easy if you remember they are juststarting  addresses and you can have segments from 0000 to ffff.
This address 0001:0000 is the exact same address in memory as 0000:000f
Increasing the segment by 1 increases the actual physical address or offset by sixteen.
The segment registers are limited too.

CS= Code Segment
DS= Data Segment
SS = Stack Segment
ES = Extra Segment

This was a convention used by intel but they can point to any type of data.
However when using them for addressing you have to use the correct ones.
You seem to want to use segments to store a variety of data.
Just use variables to hold data :cheekygreen:

Rockphorr

Quote from: dedndave on March 16, 2012, 04:21:12 PM
yah - well, the instructor probably has no idea how to write the code - lol

there are several ways to do this
you can use seperate segments or you can combine them into a GROUP
attached is an example of both ways

you could also use the .MODEL, .STACK, .DATA, .CODE "shortcut" directives
i chose to do it the old way to better illustrate how segments and groups are created

dedndave are almost intruder of no homework rule :bg :bg :bg :bg :bg :bg :bg
Strike while the iron is hot - Бей утюгом, пока он горячий


parker

guys i'm just trying to visualize and understand on how the segments works, that's why i'm gathering an info from experts like you so i could fully absorb some signifacant details.
I will take your advice P1  :U (read and read...) thanks a lot, cheers to everyone.

Quote from: dedndave on March 16, 2012, 03:10:08 PM
you should not assign "hard" values to the segments (101h,102h, etc)
instead, assing names and let the operating system assign the segment addresses
  :U key point absorbed :bg
I admire the effort of everyone who tend to share their wisdom without any kind in return. Thank you guyz...!

dedndave

#9
understanding segments isn't as difficult as one might think
if you know a little bit about early intel processors, namely the 8088 that the first IBM PC used, it is simple

the 8088 has 16-bit registers
so, when directly addressing memory with a register, you can access up to 64 Kb of address space
well - that isn't a lot of memory - although, i remember when we thought it was - lol

to extend the addressing capabilities, intel came up with the segmented addressing scheme
if you can handle 20-bit addresses, you can now address up to 1 Mb of memory space

they assigned (4) 16-bit segment registers:
CS - code segment - the IP (instruction pointer) always uses the CS register as a reference
DS - data segment - most addressing modes use this register as the default reference
ES - extra segment - used primarily for string instructions, allowing the destination segment to be different from the source
SS - stack segment - SP (stack pointer) and BP (base pointer) use this segment as the default reference

so - how do we get a 20-bit address out of 16-bit registers ?
simple - we multiply the value in the segment register by 16, effectively shifting it left 4 bits
then - 16-bit addresses may be added to the segment to come up with a 20-bit "physical" address

16-bit segment    A000
16-bit address   + F000
-----------------------
physical address  AF000


in a program, the term "segment" takes on a slightly different meaning, really
that is because the same word is used to create regions of a program - code, data, stack
the assembler allows us to assign names to these regions to make them easy to work with
a processor segment may not be the same as a program segment - this may be the source of some confusion
they are different because the assembler/linker may combine and overlap segments
we may assign several program segments to a "group" and address all of them with the same processor segment value
of course, the limitation here is that the total size of the group may not exceed the 64 Kb size

parker

dedndave,

segments.zip  wow  :U informative structure of segment  :U nice one dave!
I admire the effort of everyone who tend to share their wisdom without any kind in return. Thank you guyz...!