• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

variabled in assembler - question


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


2 Solutions
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
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,
eylkrnAuthor Commented:

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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now