50calrevolver
50calrevolver

Reputation: 142

What is the difference between .code/.data and code/data segment?

I was given a particular program code by my instructor, to sort a set of data using bubble sort in assembly language (8086 micro-processor instructions). For all previous codes I had used the syntax:

DATA SEGMENT
    <DATA HERE>
DATA ENDS
CODE SEGMENT
    ASSUME DS:DATA, CS:CODE
    START:
        <CODE HERE>
    CODE ENDS
END START

The code given by my instructor is below:

org 100h 
.data

array  db 9,6,5,4,3,2,1
count  dw 7

.code

mov cx,count      
dec cx   

nextscan:          
mov bx,cx
mov si,0 

nextcomp:

mov al,array[si]
mov dl,array[si+1]
cmp al,dl

jnc noswap 

mov array[si],dl
mov array[si+1],al

noswap: 

inc si
dec bx

jnz nextcomp

loop nextscan

mov cx,7
mov si,0

print:

mov al,array[si]  
add al,30h
mov ah,0eh
int  10h 
mov ah,2
mov dl , ' '
int 21h
inc si
loop print    

ret 

I did not understand why the data segment and code segment were replaced by .data and .code respectively and that there is (apparently) no need to end those segments. Also, the assume directive is absent and the program still works fine. What really confused me was that when I modified the program as below (changed syntax to what I was familiar with), the program did not work:

data segment

array  db 9,6,5,4,3,2,1
count  dw 7

data ends

code segment

assume ds:data, cs:code

start:
mov ax,data
mov ds,ax
mov cx,count      
dec cx   

nextscan:          
mov bx,cx
mov si,0 

nextcomp:

mov al,array[si]
mov dl,array[si+1]
cmp al,dl

jnc noswap 

mov array[si],dl
mov array[si+1],al

noswap: 

inc si
dec bx

jnz nextcomp

loop nextscan

mov cx,7
mov si,0

print:

mov al,array[si]  
add al,30h
mov ah,0eh
int  10h 
mov ah,2
mov dl , ' '
int 21h
inc si
loop print    

ret 

code ends
end start

The above code resulted in an infinite loop while running. I use emu8086, if it helps. I need help in understanding the difference between .data/.code and data segment/code segment directives and when to use which one.

Upvotes: 4

Views: 2658

Answers (1)

If you are not using the directive org 100h then you need to end your program manually, so replace ret like this :

int 21h
inc si
loop print    

;ret 
mov ax, 4c00h      ;◄■■ END PROGRAM PROPERLY AND
int 21h            ;◄■■ RETURN CONTROL TO OS.

code ends
end start

The directive org 100h organizes de program in a way it fits in only one segment and the behavior is different. If you don't use this directive then your program is a "standard" executable (DOS) separated in segments.

Actually, you should always end your assembly programs manually to ensure everything is ok, with or without org 100h.

Oh, yes, about your question, there is no significant difference between ".data/.code and data segment/code segment directives", the only difference that comes to my mind is that you have to close segments when you are using code segment while .code does not require it. The infinite loop was not caused for replacing .code by code segment, the problem was returning control to OS properly. So you can use .code or code segment as you wish.

Upvotes: 4

Related Questions