Author Topic: How to remove the last [amount] of characters from a string  (Read 17222 times)

Offline yoran

  • Jr. Member
  • *
  • Posts: 19
I want to do the following, let's say we have a string called A and it contains 'echo Hello, World!'
I want to do this:
reverse A, now it is '!dlrow ,elloH ohce'
remove the last 5 chars, so it is '!dlrow, elloH'
reverse A, so it is 'Hello, World!'
and output A, because the user inputted A in the first place in a, it is for a command prompt, so that is why the echo is there.

I can do the reversing, but I can't seem to figure out how to remove the last 5 chars from the string.. does anyone know how to do this?
It is bare bones (no int21h) and Bits 16.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to remove the last [amount] of characters from a string
« Reply #1 on: May 14, 2018, 05:52:40 PM »
Count 'em? I'm not sure I see the "issue" here. Zero-terminated string? Do you really want to remove 5 characters exactly, or "to the first space"? Is there some point to reversing it and then reversing it again? Do you propose to replace the "removed" characters with spaces? Zeros? Or can you just re-terminate the string? Seems like it should be a simple question, but we may need more information. Where do you get stuck?

Best,
Frank


Offline yoran

  • Jr. Member
  • *
  • Posts: 19
Re: How to remove the last [amount] of characters from a string
« Reply #2 on: May 18, 2018, 03:06:32 PM »
I basically have a command line, I want to be able to type 'ECHO HELLO, WORLD!', and it to repeat everything except 'ECHO '. If there is any other way, I will be happy to know!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to remove the last [amount] of characters from a string
« Reply #3 on: May 18, 2018, 06:58:48 PM »
We really don't know what you've got so far. I suppose something like...
Code: [Select]
mov si, prompt
call print
mov cx, BUFSIZ
mov di, buffer
call ngets ; please don't reinvent gets!
; ...
Now I suppose you want to see if the pesky user has typed a known command or not. Besides "ECHO", "REBOOT" is an easy one. "DIR" needs a lot more code.
If it's "ECHO" (need to be uppercase?)... suppose di is still pointing to the space...
Code: [Select]
lea si, [buffer + di] ; +1?
call print
That should about do it. Of course, I've left out a few details. I don't know how you want to do it. since I don't know what you've got so far...

Best,
Frank


Offline yoran

  • Jr. Member
  • *
  • Posts: 19
Re: How to remove the last [amount] of characters from a string
« Reply #4 on: May 19, 2018, 10:20:25 AM »
(no delete comment option, so I'll do this)
« Last Edit: May 19, 2018, 04:45:41 PM by yoran »

Offline yoran

  • Jr. Member
  • *
  • Posts: 19
Re: How to remove the last [amount] of characters from a string
« Reply #5 on: May 19, 2018, 04:44:54 PM »
I FOUND IT!
What is now do is, when I want to check for echo, is I'm going to split the command into ECHO and everything else.
DI = everything else, so mov [parameters], di, check if SI is echo, and if so:
echo_cli:
   mov WORD si, [parameters]
   cmp si, 0
   je cmd
   int 80h ;print
   jmp cmd

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to remove the last [amount] of characters from a string
« Reply #6 on: May 19, 2018, 05:33:22 PM »
Does it work? Seems unlikely... You only put two bytes into si - "he" perhaps...

Best,
Frank


Offline yoran

  • Jr. Member
  • *
  • Posts: 19
Re: How to remove the last [amount] of characters from a string
« Reply #7 on: May 19, 2018, 06:23:07 PM »
input_buffer = ECHO HELLO, WORLD!
backup_buffer = input_buffer
        >ECHO (removed)
split |
        >HELLO, WORLD! (di)
parameters = DI
check
yes? echo_cli

echo_cli:
    mov WORD si, [parameters] ;HELLO, WORLD!
   cmp si, 0 ;so not empty
   je cmd
   int 80h ;print SI, so HELLO, WORLD!
   jmp cmd

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to remove the last [amount] of characters from a string
« Reply #8 on: May 19, 2018, 07:38:58 PM »
Code: [Select]
    mov WORD si, [parameters] ;HELLO, WORLD!
This will put two bytes - the characters 'H' and 'E' - into si. I don't see how this could possibly cause "HELLO, WORLD!" to be printed, which is what I understand you want.

More likely(?) you want the address of parameters...
Code: [Select]
    mov WORD si, parameters ; 16 bit address of HELLO, WORLD!

Linux uses int 80h. The subfunction that prints text expects an address in ecx. But I unfrstood you were using a "bare bones" system. It is quite possible to install code at int 80h which prints text at the address in si ... or the "[contents"], but that would only be two bytes. Is this what you've done? We really don't know...

Best,
Frank



Offline yoran

  • Jr. Member
  • *
  • Posts: 19
Re: How to remove the last [amount] of characters from a string
« Reply #9 on: May 20, 2018, 10:58:14 AM »
parameters is defined as following:
parameters dw 0

I tested this, and it works.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to remove the last [amount] of characters from a string
« Reply #10 on: May 20, 2018, 05:59:23 PM »
Okay. If it works, it works!

Best,
Frank



Offline yoran

  • Jr. Member
  • *
  • Posts: 19
Re: How to remove the last [amount] of characters from a string
« Reply #11 on: May 21, 2018, 11:33:56 AM »
Anyway, thanks for helping!