Author Topic: Wacky executable output...  (Read 6119 times)

Offline dominick

  • Jr. Member
  • *
  • Posts: 9
Wacky executable output...
« on: July 25, 2011, 07:29:13 AM »
Hi,

 I'm creating a compiler and I've successfully gotten it to create a .com file using nasm. I've also gotten it to create an .exe file but the output of the produced executable is gibberish, so I guess it's reading the wrong part of memory? I'm getting no errors from nasm or alink at the moment.

Test.asm:

global main
segment stack stack
st resb 100
[section .text]

main:
mov ax, stack
mov ss, ax
mov sp, 100

mov esi, CD0
call putstr
mov esi, CD1
call putstr
mov ax, 0x4c00
int 0x21
putc:
  mov ah, 0x0e
  int 0x10
ret
putstr:
    lodsb
    cmp al, 0x00
    je .end
    call putc
    jmp putstr
.end
    ret

CD0 DB "test", 0
CD1 DB 13, 10, 0


 and the commands I'm executing for this code are:
  nasm -fobj %1 -o test.obj ; %1 being the name of the file.
  alink -oEXE -entry main test.obj -o test.exe


If anyone could help me out with this problem, I'd be really grateful,

 Dominick

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Wacky executable output...
« Reply #1 on: July 25, 2011, 07:55:08 AM »
When dos loads a .com file, it sets ds and es to your one-and-only segment. When dos loads an .exe, ds and es are pointing to your PSP segment, so you'll need to set 'em to your data segment. In this case, your data's in your code segment, so...

Code: [Select]
push cs
pop ds

If you'd had a separate data segment:

Code: [Select]
mov ax, data
mov ds, ax
; maybe es, too...

Dos should have ss and sp all set up, so you don't really need to do that...

Best,
Frank


Offline dominick

  • Jr. Member
  • *
  • Posts: 9
Re: Wacky executable output...
« Reply #2 on: July 25, 2011, 05:16:30 PM »
 Frank, thank you for your information and the helpful code. I added a data segment to make the code a little bit more organized. The lines for handling the stack seem to satisfy alink's complaints about the stack.
 If anyone has a similar problem to this and would like to see the modified code I've posted it below...

global main
segment stack stack
   st resb 100
[section .text]

main:

mov ax, data
mov ds, ax

   mov ax, stack
   mov ss, ax
   mov sp, 100

mov esi, CD0
call putstr
mov esi, CD1
call putstr
mov ax, 0x4c00
int 0x21
putc:
  mov ah, 0x0e
  int 0x10
ret
putstr:
    lodsb
    cmp al, 0x00
    je .end
    call putc
    jmp putstr
.end
    ret

[section .data]
CD0 DB "test", 0
CD1 DB 13, 10, 0