flat assembler
Message board for the users of flat assembler.

Index > DOS > EXE vs COM | Dos Read String/Output String Error

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby 10 Mar 2012, 21:09
Hello, I am new to dos Shocked and am working on a basic dos code inspired by: http://www.skynet.ie/~darkstar/assembler/tut5.html

Only I want mine to read a 15 character input and then output it to the screen. Nothing too hard. However, I keep getting an invalid operand error at

Code:
mov b[bx],'$'    


My full code is below:

Code:
format binary as "exe"

org 0x100

Start:

;Setup String ------------------------------------------------------

mov bx,offsetnumber+7        ; put a $ at end of buffer.
mov b[bx],'$'                 ; we will fill buffer from back forwards
dec bx

;Input String ------------------------------------------------------

mov dx,offsetnumber
mov ax,dx
mov b[bx],15
int 21h
mov [bx], ax

;Print String--------------------------------------------------------

mov ax,09
int 21h

;Exit ---------------------------------------------------------------

mov al,00
int 21h
    



Thanks in advance for your help!!!!!

EDIT by DOS386 : enhanced subject
Post 10 Mar 2012, 21:09
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 10 Mar 2012, 21:33
FASM does not understand what the operand b is, instead the FASM syntax is:

Code:
mov byte[bx], "$"            


FASM uses stuff like "byte" and "word" instead of "b" and "w". The reason it is different is because every assembler has different syntax (kinda like C and C++ only not quite).

_________________
http://codercat.org/
Post 10 Mar 2012, 21:33
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 10 Mar 2012, 21:42
Also note that FASM does not use the "offset" operand!

So when you type:

Code:
start:
  mov dx,offset number
  mov bx,dx
    


Do not type offset instead just type:

Code:
start:
  mov dx, number
  mov bx,dx
    

_________________
http://codercat.org/
Post 10 Mar 2012, 21:42
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby 10 Mar 2012, 22:03
Great, thanks!!! Almost compiles, but one last error at:

Code:
mov byte[bx],ax    


Operand Sizes Do Not Match =(

Should I change the ax register? If so, to what? If not, what do I do to fix?
Post 10 Mar 2012, 22:03
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 10 Mar 2012, 22:15
I might start with a more simple tutorial on assembly language.

AX is a 16bit register.
byte tells the assembler you want to move 8bit data into [bx]

thus you can not tell FASM to move 16bit as an 8bit data Very Happy

byte == 8bit data
word == 16bit data

also, you may cheat FASM with simply

Code:
mov [bx], ax    


as FASM already knows that AX is 16bit Very Happy HOWEVER it is good to note
using this in your code will not give you your desired result Wink

=====================================================

Here, I started with this ZIP tutorial:


Description:
Download
Filename: ALLTUTS.ZIP
Filesize: 84.13 KB
Downloaded: 574 Time(s)


_________________
http://codercat.org/
Post 10 Mar 2012, 22:15
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby 10 Mar 2012, 23:14
Thanks for the tutorial! However, I would like to finish my code below before I change tutorials =)

I got the following code to compile, but it will not stay open (even when I remove the exit code). When I enter it in cmd, and then a number or two after it, the program will do nothing Sad

What do I need to finish it!?

Code:
format binary as "exe"

org 0x100

Start:

;Setup String ------------------------------------------------------

mov bx,dx ; put a $ at end of buffer.
mov word[bx],'$' ; we will fill buffer from back forwards
dec bx

;Input String ------------------------------------------------------

mov bx,dx
mov ax,dx
mov word[bx],15
int 21h
mov word[bx],ax

;Print String--------------------------------------------------------

mov ax,09
int 21h

;Exit ---------------------------------------------------------------

mov al,00
int 21h
    
Post 10 Mar 2012, 23:14
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 10 Mar 2012, 23:23
It is exiting to fast before you can see it, try launching it from the command prompt. or before any exitcode add this:

Code:
stay:
      jmp stay    


Of course the program will only exit it if you force quite, or if you are running pure DOS, just hit the off switch. Wink

_________________
http://codercat.org/
Post 10 Mar 2012, 23:23
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 10 Mar 2012, 23:26
add this code after or before ;Exit --------------------------------------
Code:
xor ax, ax
int $16
    
Post 10 Mar 2012, 23:26
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 10 Mar 2012, 23:30
Coty wrote:
pure DOS, just hit the off switch. Wink


lol Very Happy
Windows 8 is similar to pure DOS Wink

Offtopic: by the way Windows 8 saved my computer. I was making a personal firewall that hooks all processes on startup and I kept BSODing my System even in Safe mode and recovery console. So I used that Win8 I got from you to repair my PC back. Cool story
Post 10 Mar 2012, 23:30
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 11 Mar 2012, 00:04
^ No because people actually WANTED DOS Surprised

And you used windows 8 to repair XP? Very Happy I'd be afraid it would screw something up Shocked

_________________
http://codercat.org/
Post 11 Mar 2012, 00:04
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 11 Mar 2012, 01:34
Try this:

Code:
format binary as "EXE"

org 0x100

Start:

;Print String--------------------------------------------------------
mov dx,msg
mov ah,09
int 21h

mov bl,10

loop1:
mov ah,01
int 21h
dec bl
jnz loop1

jmp exit
;Exit ---------------------------------------------------------------

msg     db 'DOS started ...,',0dh,0ah,'$'

exit:
    


You need to put function of int 21 in ah value, not ax.
This is not the same.
That's why you not see something.
If you put 09h in ax, then al will have 09h and ah will have 00h which means exit.

The program prints a starting message and you can type in 10 chars/keys, then program stops/ends. Find out how it works. Very Happy
Post 11 Mar 2012, 01:34
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 11 Mar 2012, 01:58
Just one thing here. This line is entirely unneeded:
Code:
format binary as "exe"    
Remove this line and let fasm create a .com file.
Post 11 Mar 2012, 01:58
View user's profile Send private message Visit poster's website Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby 11 Mar 2012, 03:54
@shutdownall: Thanks for your code! I changed ax to ah, and I am closer still (error posted below)! I did not know that I was forcing ax to close the program Embarassed

How does the bl register control the # of chars?

The ah register is set at 09 so user can input, and the 01 is for each letter entered one at a time in the loop, but the value is not stored in ah, right?

How would you get the ah register to store the chars for when the user hits enter? Would that be the byte/word statements, followed by the number of charectors in the string with ah in []? So if I would have your program store the input I would change the 01 from the loop to 09? And then add a variable equal to zero, which ax would move the value to, and then move that to dx after enter is called (after first int21h)?



@revolution: I would like the file to be executable, not in .com format. Thanks though.



@typedef & Coty: I tried your codes, but they did not change anything. Sad




It does something now at least, and that thing is outputting random symbols and beeping once when you uncomment my line with ax/db Confused . Now what am I doing wrong? Just overloading a register, or corrupting it with a bad value!?


Code:
format binary as "exe"

org 0x100

Start:

;Setup String ------------------------------------------------------

mov bx,dx       ; put a $ at end of buffer.
mov byte[bx],'$' ; we will fill buffer from back forwards
dec bx

;Input String ------------------------------------------------------

mov bx,dx
;mov ax,dx THIS IS THE LINE THAT CAUSES BEEPING AND SYMBOLS!!!!!!!
mov byte[bx],15
int 21h
mov byte[bx],ah

;Print String--------------------------------------------------------

mov ah,09
int 21h


;stay:
 ;     jmp stay

;Exit ---------------------------------------------------------------
;xor ax, ax
;int $16
mov al,00
int 21h
    
Post 11 Mar 2012, 03:54
View user's profile Send private message Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby 11 Mar 2012, 04:31
@shutdownall: tried to get your code to display 0 after any string has been entered, only the zero will display over and over again once a char is entered. Well, it has the beeping and random chars (charector) outputs over and over again Mad. Just like my code above Sad

Code:
format binary as "EXE"

org 0x100 

Start: 

;Print String-------------------------------------------------------- 

mov ah,09   ;start with input
int 21h    ;call the interupt
dec bl           ;have dl register limit string to 10 chars
mov bl,10     ;finishing ^
mov dx, msg  ;has dx display 0, in theory  Laughing 
int 21h  ;interupt

msg  db '0' ;the zero, held in a variable


    


So what can I do to fix both of my codes, anyone?
Post 11 Mar 2012, 04:31
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 11 Mar 2012, 05:45
You said you want an EXE, Console

Code:
format PE CONSOLE       ; EXE,

include 'win32ax.inc'

entry main

section '.txt' code readable executable

main:
     invoke SetConsoleTitle,'My cool console app'
     invoke GetStdHandle,STD_INPUT_HANDLE

     mov [hConsole], eax
     invoke GetStdHandle,STD_OUTPUT_HANDLE
     mov  [hConsole+4], eax

     ; read something,  about 255 bytes max
     invoke ReadFile,dword[hConsole+(0*4)],pszBuffer,255,ebx,NULL

     ; write it to the ouput handle
     ; ebx = bytes read
     invoke WriteFile,dword[hConsole+(1*4)],pszBuffer,dword[ebx],ebx,NULL

     cinvoke system,'pause'

     ret


section '.idata' import data readable

library kernel32,'kernel32.dll',msvc,'msvcrt.dll' ; <---- Standard version

import msvc,\
       system,'system'

include 'api/kernel32.inc'

section '.dada' data readable writeable

pszBuffer db 256 dup(0) ; 255
hConsole:
        dd 0 ; STD_IN
        dd 0 ; STD_OUT




    
Post 11 Mar 2012, 05:45
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 11 Mar 2012, 05:46
But this is DOS section ?

So you actually want an MZ, but wait and MZ is .COM

WTF ?!
Post 11 Mar 2012, 05:46
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 11 Mar 2012, 06:30
Ok, I'll jusy lay them out you'll chose the one you like. Here's something for you to study this weekend.

Code:
org $100

    mov  bx, prompt
    mov  cx, plen
    call prints

    mov  bx, buffer
    mov  cx, 90
    call gets

    mov cx, ax      ; use returned number of bytes read
    mov bx, buffer
    call prints

    call getc

    call quit

; bx = buffer
; cx = len
prints:

.p:
   cmp  cx, 0
   je   .d
   mov dl, byte[bx]
   mov ah, 02
   int $21

   inc bx
   dec cx
   jmp .p
.d:

ret

buffer db 1024 dup(0),'$'
prompt db 'PASSWORD: '
plen = $ - prompt

quit:
   mov ah,$4c
   int $21
ret

; cx = buffer length
; bx = buffer address
; terminate when $D is encountered
; returns number of bytes read
gets:
    push    cx
.get:
    push cx       ; save on cross call
      call getc
    pop  cx

    cmp  al, $D   ; return
    je   .fin

    mov  byte[bx], al
    inc  bx

    dec cx
    jnz .get
.fin:
    pop  ax
    sub  ax,cx   ; ax = bytes read
ret

; al = key read
getc:
    xor ax, ax
    int $16
ret
    
Post 11 Mar 2012, 06:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 11 Mar 2012, 08:10
drewtoby wrote:
@revolution: I would like the file to be executable, not in .com format. Thanks though.
Then you want to use:
Code:
format mz    
But then you will need to include sections references and you wouldn't want to use org 0x100. See the "multiseg" MZ file example in the fasm download example folder.

What you have now is a .com file but with a .exe name. You are being fooled into thinking you have a .exe file, when actually you don't.


Last edited by revolution on 11 Mar 2012, 09:01; edited 2 times in total
Post 11 Mar 2012, 08:10
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 11 Mar 2012, 08:12
typedef wrote:
So you actually want an MZ, but wait and MZ is .COM

WTF ?!
MZ format is a .exe file.
Post 11 Mar 2012, 08:12
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 11 Mar 2012, 09:26
revolution wrote:

> Then you want to use:
> format mz
> But then you will need to include sections references
> See the "multiseg" MZ file example

NO. Segments/sections are not obligatory.

Don't look at typedef's posts, they are bullshit.

DOS COM - format binary, org $0100

DOS MZ EXE - format MZ or (format binary but brew headers manually), org 0

Win32 PE EXE - format PE or (format binary but brew headers manually), org $0040'1000

DOS is dumb so your "format binary org $0100" may work, but it's faulty.

Anyway, make sure to use proper format and proper subforum matching your OS Idea

If you want to use Windows 8 (or any other Windows version from - infinity to + infinity) then use format PE , see included example or http://board.flatassembler.net/topic.php?t=11172

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 11 Mar 2012, 09:26
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.