Click to See Complete Forum and Search --> : assembly with nasm... need a little help


binaryDigit
08-30-2001, 12:39 AM
i have a book on assembly. unfortunately it focuses on 16bit programming. so i've been looking around at linuxassembly.org and nasm's site. but i'm having a really hard time figuring things out.

consider the following book example

var_i dw 0
var_k dw 0

mov var_i, 10
mov var_k, 13
mov ax, var_i
add var_k, ax


pretty simple. but reading nasm's site content tells me that unless i'm using brackets around my variables that i'm accessing a memory location. and that i have to specify a length.
so this is the code that i actually got to assemble


section .text
global _start

int_i dd 0
int_k dd 0

_start:

mov dword [var_i], 10
mov eax, [var_i]
mov dword [var_k], 13
add dword [var_k], eax



now it assmebles but i get a seg fault.
any help.

JasonC
08-30-2001, 12:05 PM
Its been awhile for assembly but... I believe the ADD instruction has to have a register as the destination i.e. add ax,var1

Stuka
08-31-2001, 12:00 AM
Actually, by putting the brackets around the variable, you're trying to use it as a pointer - sorta like the & operator in C. Your code is trying to access very low-numbered memory locations. What kind of errors were you getting when it didn't assemble? Were you using a segmented (16 bit) memory model w/o setting up your segments? There could be any number of answers here....
Ahh, the joys of assembly! :D

Niminator
08-31-2001, 02:03 AM
How are you compiling it? Are you making sure you are returning pushing and popping all of the appropriate things? And is that a complete program? It doesn't look like one...

augur
08-31-2001, 05:55 AM
Greetings,

It seems that the problem here is that you don't tell the OS when the program ends. You need to explicitly make a system call instructing the OS to end the program. Kind of:
mov ax, 4c00h
int 21h
Of course that was in the DOS days. In Linux it would be something like:
mov eax, <syscall_that_ends_program>
int 80h

Unfortunatly i dont recall what the <syscall_that_ends_program> is in Linux!
Apart from that everything seems to be ok, although i'm not sure that creating variables and modifying them in the code segment is ok, since code segments are read and execute only! U could try the above sugestion, plus declaring your variables in a data segment, like this:

section .data
int_i dd 0 ; should this be var_i?
int_k dd 0 ; and this var_k ?

section .text
global _start
_start:
mov dword [var_i], 10
mov eax, [var_i]
mov dword [var_k], 13
add dword [var_k], eax

mov eax, <syscall_to_end_prog>
int 80h

Hope this helps.