NASM Forum > Programming with NASM
Variable isn't what I expected.
tysonprogrammer:
I a newish to assembly and nasm. Iw as reading a book on masm assembly to learn assembly and so I thought I had a good handle on addressing, guess not. Anyway I wrote this little program that is supposed add two numbers to see if I could figure it out.
--- Code: ---; add.a
bits 16 ; tell nasm how many bits we are using
segment data
val1 db 10h
val2 db 20h
segment code
..start:
xor al, al ; zero out al
mov al, [val1]
; add al, [val2]
exit:
mov ah, 4ch ;exit
int 21h ;call intr
segment stack class=stack
resb 512 ; 64 is a little to little for interrupts
--- End code ---
When I run this program through a debugger I am not seeing the value for val1, and I am so confused. I am using the Open Watcom debugger. Like to screenshot is below. Instead of 10h I am seeing CD as the value.
https://www.dropbox.com/s/ag87rirnue5z7pe/Screen%20Shot%202020-09-20%20at%207.46.30%20PM.png?dl=0
I am using nasm on FreeDOS running in VirtualBOX.
any help would be appreciated.
thanks,
Tyson
debs3759:
Try the following:
--- Code: ---bits 16
section .data ; I always use section in nasm, can't remember if segment is handled the same
; Data section is named .data for nasm
; In your code, presumably because of section/segment names, data is placed before the code section.
val1 db 10h
section .text ; nasm calls the code section .text
org 100h ; needed if creating a .com binary
;..start ; nasm doesn't need this line for a DOS binary
mov dx,cs
mov ds,dx ; set data segment to be the same as code segment for .com files
; without this, the data will be loaded from the wrong area of memory
xor al,al
mov al,[val1]
exit:
mov ah,4ch
int 21h
--- End code ---
As you are not using the stack, you don't need to set it up. If you do start using it, you need to explicitly set it up. I can't remember whether the manual explains how to do this, but in most DOS .com executable, it can be the same as CS as well, then will need a cld instruction.
All this is from memory, so if it doesn't work for you I will check the basics. It should be right though, and om program exit, AL should still contain the last value placed in it, as a return code.
Frank Kotler:
Hi Tyson,
Welcome to the forum.
This is embarrassing. My memory is all shot.
"..start" is a special symbol known only to "-f obj". If that's what you're using, it won't like ''org'' as Debs suggests. You will want the stack. To do as Debs suggests:
--- Code: ---nasm -f bin myprog.asm -o myprog.com
--- End code ---
No linker required.
To print the errorlevel/exitcode:
--- Code: ---echo %errorlevel%
--- End code ---
I'm not sure about the % signs. (Told you this was embarrassing.)
I can't vouch for your debugger and I'm confused by your screenshot, I see "10" (doesn't say that it's hex, but probably is) I don't see "cd". Your code looks good to me. So I'm confused!
What I would do is do the addition. 30h is ascii code for '0' (the character, not the number 0). Print it:
--- Code: ---mov ah, 0eh
int 10h
--- End code ---
That should put "0" on your screen.
If none of that helps, get back to us and make us do it!
This is embarrassing.
Best,
Frank
avcaballero:
Hello, this should work:
--- Code: ---[org 100h]
[section .text]
inicio:
MOV AH, 0Eh
MOV AL, 9 ; byte you want to print
OR AL, 30h
INT 10h
RET
--- End code ---
Compile with:
>nasm -fbin Program.asm -o Program.com
debs3759:
--- Quote from: Frank Kotler on September 21, 2020, 02:38:32 AM ---I can't vouch for your debugger and I'm confused by your screenshot, I see "10" (doesn't say that it's hex, but probably is) I don't see "cd". Your code looks good to me. So I'm confused!
--- End quote ---
Biggest problem I see is that he didn't set up DS, so it's not pointing at his data segment.
Navigation
[0] Message Index
[#] Next page
Go to full version