variabled in assembler - question

Posted on 2005-03-25
Medium Priority
Last Modified: 2008-03-10

I have a question , about variables in masm.
I've coded the following program(attached). the program get 4 numbers from num1,num2,num3,num4 and print them on the screen at the corrdinate of dx.
anyway , I'm  experiencing some wierd problems:

1) why can't I declare the variables in .data , the program doesnt work the same. why?
2) I cant debug my program if I decalre the variables at code segment. I see the machine code instead of my program. why?
3) how can I declare the variables in the data segment and that the program will run ok.
4) if I add another variables (for examples: num db 1) before the num1 variables then the program is running ok, why?

Eyal Keren

my program:

.model small
.stack 100h


num1 db 5h
num2 db 3h
num3 db 8h
num4 db 2h

mov ax,02h
int 10h

add num1,30h ;converting the number wanted to its ascii code
add num2,30h
add num3,30h
add num4,30h

mov bx,0
mov ax,0b800h
mov ds,ax
;mov ax,3441h
;mov [bx],ax

mov al,030h ;  COLOR
mov dx,0000h ; CORDINATES "0000h" is considerred to be first line,first column

cmp dl,0 ;checks if x-axis is zero, no need in shifting inside the row
jz yy
call x_pos ;else, calling the x-shifting procedure

yy:cmp dh,0 ;checks if y-axis is zero, no need in shifting rows
jz zz
call y_pos ;else, calling the y-shifting procedure

zz: ; printing on the screen after going to the right cordinates

mov ah,al
mov al,[num1] ;should be num1 insteed, and its value and not ascii code
mov [bx],ax
add bx,2
mov al,[num2] ;should be num2 insteed, and its value and not ascii code
mov [bx],ax
add bx,2
mov al,[num3] ;should be num3 insteed, and its value and not ascii code
mov [bx],ax
add bx,2
mov al,[num4] ;should be num4 insteed, and its value and not ascii code
mov [bx],ax

kkk: jmp kkk ; stuck the program, so we can see the result on the screen

x_pos proc
;dec dl
loop1: add bx,2
       dec dl
      jnz loop1

x_pos endp

y_pos proc
;dec dh
loop2: add bx,0a0h
      dec dh
      jnz loop2

y_pos endp

mov ah,4ch; Exit Code
int 21h


Question by:eylkrn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Assisted Solution

dimitry earned 400 total points
ID: 13631928
1) Try to declare varialbes at the END of your code, not at the beginning.. Where do you think is your code entry point ? ;)

mov ah,4ch; Exit Code
int 21h

num1 db 5h
num2 db 3h
num3 db 8h
num4 db 2h


2) To access data in data segment you need to initialize DS segment register

Accepted Solution

mzvika earned 1600 total points
ID: 13632033
I've got several remarks:
1) all variable addresses are, by default, offsets in to the data segment. the data segment address is held in DS.

in those two lines:
        mov ax,0b800h
        mov ds,ax

you are changind DS, thus changing your data segment. your variables references are now accessing the wrong memory addresses.

2) in the 'end' statement, you should tell the compiler where your program execution starts. if you don't it will run the program from the beginning of your code segment, which means that your variables are treated as code - this is NOT cool.

3) tell the compiler about your segments assumptions:
assume      cs:.code, ds:.data

4) if you do put your variables in the code segment, you need to initialize your DS register
assume      cs:.code, ds:.code
push cs
pop ds

5) don't put your procedure in the middle of the "main" program. that is, move the "int 21h" call above your x_pos procedure. don't assume the compiler will rearrange your code for you...

have fun,

Author Comment

ID: 13636716

thanks zvika for your remark.

I've solved my problem with anothed method:

num1 db 1dh
num2 db 19h
num3 db 22h
num4 db 19h
mov ax, @data
mov es,ax

and now I can call my variables using es:num1, es:num2 and so on.



Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question