Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8086 Assembly Programing problem

Status
Not open for further replies.

lord loh.

Member
I have written a small 8086 programme to display a string on the PC screen using MS DOS interrupts....

I would like to have my string "Hello World !" stored in the data segment...

My assembler a86/nasm demands the datasegment to be defined before the code segment...

The code is generated and when I see the .com file, the string is defined before the code.... And in DEBUG Utility, the string is placed at cs:0100 and the dat a is executed before the code.... I guess that the programming is giving the desired output only by fluke...

Please help....
Code:
.DATA SEGMENT

hm:
 db 'Hello World'
.DATA ENDS

.CODE SEGMENT
mov ax,SEG hm
mov ds,ax
mov si,OFFSET hm
mov cx,0005h
mov ah,02h
writestring:
mov dl,[si]
int 21h
inc si
loop writestring
int 20h
.CODE ENDS

Debug Output
Code:
D:\a86>debug 1.com
-d
0B5B:0100  48 65 6C 6C 6F 20 57 6F-72 6C 64 8C C8 8E D8 BE   Hello World.....
0B5B:0110  00 01 B9 05 00 B4 02 8A-14 CD 21 46 E2 F9 CD 20   ..........!F...
0B5B:0120  75 06 C7 06 7E 01 FF FF-39 3E 80 01 75 05 6A 00   u...~...9>..u.j.
0B5B:0130  E8 0F 12 6B DF 0E 8B B7-2E 4B C1 E6 02 03 36 B4   ...k.....K....6.
0B5B:0140  52 EB 18 8B C6 2B 06 B4-52 C1 F8 02 50 E8 66 A1   R....+..R...P.f.
0B5B:0150  8B 04 C1 E0 02 03 06 B4-52 8B F0 83 3C FF 75 E3   ........R...<.u.
0B5B:0160  6B DF 0E C7 87 2E 4B FF-FF 33 C0 5E 5F C9 C2 02   k.....K..3.^_...
0B5B:0170  00 00 55 8B EC 56 8B 5E-04 6B DB 0E 8D 87 2E 4B   ..U..V.^.k.....K

Unassembled
Code:
-u
0B5B:0100 48            DEC     AX
0B5B:0101 65            DB      65
0B5B:0102 6C            DB      6C
0B5B:0103 6C            DB      6C
0B5B:0104 6F            DB      6F
0B5B:0105 20576F        AND     [BX+6F],DL
0B5B:0108 726C          JB      0176
0B5B:010A 64            DB      64
0B5B:010B 8CC8          MOV     AX,CS
0B5B:010D 8ED8          MOV     DS,AX
0B5B:010F BE0001        MOV     SI,0100
0B5B:0112 B90500        MOV     CX,0005
0B5B:0115 B402          MOV     AH,02
0B5B:0117 8A14          MOV     DL,[SI]
0B5B:0119 CD21          INT     21
0B5B:011B 46            INC     SI
0B5B:011C E2F9          LOOP    0117
0B5B:011E CD20          INT     20
 
.ORG is not recommended while writing 8086 codes in MS DOS as the codes are supposed to be relocatable....

I am looking for mydata to be at the end of the code segment...
This works if I define lables and use the lables as the address locations of the data. How evcer variables are not forward referenced by any of the assembles that I have used...

For example
This code works....
Code:
mov ax,SEG datasegment
mov ds,ax
mov si,OFFSET datasegment
mov dl,[si]
.
.
.

datasegment:
db "Hello World!"

This code
Code:
mov ax,SEG msg
mov ds,ax
mov si,offset msg
mov dl,[si]
.
.
.

datasegment:
msg db "Hello World!"
generates
Code:
0B5B:0100 8CC8          MOV     AX,CS
0B5B:0102 8ED8          MOV     DS,AX
0B5B:0104 BE0B01        MOV     SI,010B
0B5B:0107 8A14          MOV     DL,[SI]
0B5B:0109 CD20          INT     20
It does not define the data segment seperate... see the mov ax,cs and mov ds,ax

:(

NASM was creating a problem while defining locations as it was not permitting the file preamble created by DOS. I had to add 100h to my locations for the programme to work correctly.
 
Are you locked into the assembler? I have MASM (Microsoft) and also, I think a free one call masm32.

Where is a link the nasm. I looked and found a net ASM.. I will try it here. Have not done x86 code in a while, but like riding a bike :)

If I recall, the .com was less that a certain size, and the .exe (and maybe .com) files has a header to them, code started at 100h.

I have lost hard drives and Windows so much, all my old .ASM files are long gone and on backup media I no longer can read :D
 
**broken link removed**

Demonstrates a way out... I do not like it... It does not allow one 256Kb space (CS+DS+ES+SS) as the data segment and the code segment overlap.... I do not have such large programs to write but I would like to incorporate good practice.
 
He's using NASM, the open source netwide assembler.

NASM's syntax is quite different to MASM, it doesn't support OFFSET or SEG, it doesn't even use labels or data types, it purely uses addresses which is better in my opinion.

For example to reference an address:

section .code
org 100h ;Mandatory, since it will assume it's a .sys file and use 0h.
Mov SI, Msg

section .data
Msg: db 'hello world'

I'm a bit rusty, I haven't done assembly programming in DOS for years as it's long been obsolete, there again it's good for learning about how computers work so it's a good thing to do.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top