flat assembler
Message board for the users of flat assembler.

Index > DOS > Simplest Way To Add A Dollar Sign On A String?

Author
Thread Post new topic Reply to topic
nitt



Joined: 27 Aug 2011
Posts: 13
Location: United States
nitt
Such as:

Code:
org 100h

mov si, 82h
mov ah, 09h
mov dx, si
int 21h
int 20h    


This will display your command line arguments, but if the user types in like "cmd hi" (assuming your file is named cmd.com), it will give you a bunch of crap, because the string doesn't automatically have a terminator at the end. That means, if they type in "cmd hi$', it will work just fine.

So, what's the simplest way to add a dollar sign to the end of si?
Post 03 Sep 2011, 04:04
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
Moved to DOS forum.
Post 03 Sep 2011, 04:10
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Here's a little something

CS/DS:80h = Argument count
CS/DS:81h = Space between arguments and COM file path.
CS/DS:82h = Start of arguments

So the thing is to get the count

Code:
org 100h

;first of all, get the length of commandline params
       xor ax,ax
       mov si,80h
       lodsb
;AL = Count, now check if there are any
    test al,al
    jz   no_params

        xor cx,cx
        mov cl,al

;else display them
      mov si,string+26
      call prints

        lea si, [DS:81h]      ;<----- Notice 81h not 82h
        call prints_cnt
        jmp ok

        no_params:
        mov si,string
        call prints

ok:
        xor ax,ax
        int 16h
        ret

string db 'No parameters specified !',0
       db 'Here are your parameters: ',13,10,0


;
;  Uses count
prints_cnt:
  ;using cl as counter
        push cx
get:
        pop cx
        cmp cl,-1
        je  done_get
        dec cl
        push cx
        lodsb
        mov dl,al
        call printc
        jmp get
done_get:

ret

;
; dl = Character
printc:
        mov ah,02
        int 21h
ret

; SI = string pointer
prints:
A:
        lodsb
        test al,al
        jz  done
        mov dl,al
        call printc

        jmp A
done:

ret

    
Post 03 Sep 2011, 06:15
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
try BIOS interrupts instead ? Rolling Eyes
Post 03 Sep 2011, 13:08
View user's profile Send private message Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239
Use DOS function 40h and find the length of the string.
Code:
org 100h
start:
        mov si, msg
        call lenstr
        mov ah, 40h
        mov bx, 1
        mov dx, si
        int 21h
        int 20h
msg db  'Hello!', 0
lenstr:
        push si
        xor cx, cx
@@:
        lodsb
        cmp al, 0
        jz @f
        inc cx
        jmp @b
@@:
        pop si
        ret
    
Post 04 Sep 2011, 06:09
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 713
Location: Adelaide
sinsi
Code:
org 100h
 mov edx,81h
 movzx ebx,byte[edx-1]
 mov byte[edx+ebx],'$'
 mov ah,9
 int 21h
 int 20h
    
Post 04 Sep 2011, 06:35
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
@sinsi Try to do it with 16 bit registers. Wink
Post 04 Sep 2011, 15:05
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
It's not a good idea to use ah=9 to print a user-inputted string, even if you $-terminate it, because the string can itself contain a $ and then it'll get truncated.
Post 04 Sep 2011, 20:32
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Goplat wrote:
It's not a good idea to use ah=9 to print a user-inputted string, even if you $-terminate it, because the string can itself contain a $ and then it'll get truncated.


+1
Post 04 Sep 2011, 21:14
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
Get the argument and pass it to DX.
Code:
org 100h
use16

start:
        xor     bx,bx               ; clear high & low bytes
        mov     bl,[80h]            ; move the length into BX
        mov     byte[82h+bx+1],'$'  ; move '$' right after the argument
        mov     ah,9                ; we want to display string
        mov     dx,82h              ; message to display
        int     21h                 ; display command-line

        ; wait for keypress
        xor     ax,ax
        int     16h
exit:
        int     20h                 ; return to OS
    


And for Goplat's +1 (thanks typedef):
Code:
org 100h
use16

start:
        xor     bx,bx               ; clear high & low bytes
        mov     bl,[80h]            ; move the length into BX
        mov     byte[82h+bx],0      ; move 0 this time since we are using BIOS interrupt
        mov     si,82h              ; message to display
        call    printString

        ; wait for keypress
        xor     ax,ax
        int     16h
exit:
        int     20h                 ; return to OS

printString:
    .getchar:
        lodsb
        or      al,al
        jz      .finished
        mov     ah,0Eh
        int     10h
        jmp     .getchar
    .finished:
        ret                         ; return
    

Where you see this line: mov byte[82h+bx],0 ; move 0 this time since we are using BIOS interrupt, I removed the 1 because it brought up letters that were not on command-line. I don't know why. If someone could explain that part for both of us, please.

_________________
meshnix
Post 05 Sep 2011, 03:05
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
adroit wrote:
Where you see this line: mov byte[82h+bx],0 ; move 0 this time since we are using BIOS interrupt, I removed the 1 because it brought up letters that were not on command-line. I don't know why. If someone could explain that part for both of us, please.

Really, if you want to nul-terminate the string, it should be [81h+bx], since the command line starts at 81h. If you write to [82h+bx], then you'll write the 0 byte after the 0Dh terminator character; if you write to [82h+bx+1] then you'll write it after the 0Dh plus whatever the next character happened to be (probably something left over from a previous command line).
Post 05 Sep 2011, 18:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
You don't have to null terminate the string at all. Just use 0xd as your terminating character in the printString subroutine.
Post 05 Sep 2011, 21:04
View user's profile Send private message Visit poster's website Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
Goplat wrote:

Really, if you want to nul-terminate the string, it should be [81h+bx], since the command line starts at 81h. If you write to [82h+bx], then you'll write the 0 byte after the 0Dh terminator character; if you write to [82h+bx+1] then you'll write it after the 0Dh plus whatever the next character happened to be (probably something left over from a previous command line).
Oooh! Thanks, man. [81+bx] would have the whitespace but that's not to much of a problem.
revolution wrote:

You don't have to null terminate the string at all. Just use 0xd as your terminating character in the printString subroutine.
This true. But does it work if the command is in a one-line script?
Post 06 Sep 2011, 14:38
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.