1

I am trying to make a program in which the user will need to type a command in a certain format to preform a calculation, for example, "get a number in Hexa,get another number in binary,subtract them,represent in decimal".

in order to do this I want to take each sentence until the char ','for example: "get a number in Hexa"

and then I will know to which function to jump, I thought about going over the input in a loop and just add it to a variable in which I will compare string with but do know how, here is what i wrote so far:

.model Small
.data
Table db "get a number in decimal,"," get a number in binary,"," get a number in hexa,"," get another number in binary,", "get another number in  decimal,", "get another number in  hexa"," multply them,"," sum them,", "subtract them,","divide them,","or them,","and them,","xor them," ,"not them,","represent in hexa,", "represen in binary,", "represent in decimal,"                                                                                                                                                                                                                                                                                                                                                     
Num dw ?
Num2 dw ?
Result dw ?
Num_count db 1
ourbuffer db ?
temp db ?
FirstS db "Please enter a the command in the following pattern: get a number in hexa,get another number in binary,substract them,represet in decimal",13,10,"$"
.stack 100H
.code
start:
mov AX,@data
mov DS,AX
mov di,0
mov si,0
instructions:
mov  dx,offset FirstS ->print instruction to the user
mov  ah,09h
int  21h 
setBuffer:
mov bx,offset ourBuffer-set buffer
mov dx,bx
mov  byte [bx],128
ScanInitialString:->get the string from the user
mov  ah,0Ah
int  21h
reset:
mov si,0
mov DI,0
Extract:->here i want to extract each sentance into temp and then compare with Table
MOV Al,0
mov Al,ourbuffer[si]
mov temp[DI],Al
add SI,2
ADD DI,2
jmp Extract








Exit:
    mov ax, 4c00H
    int 21H  
    end Start

please assist me?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Just like strcpy, but with the specified char as a terminator instead of 0. – Peter Cordes Jun 20 '21 at 02:57
  • Thank you for answering, would you be able to give me an example? stcpy is C function, I would want to go on each char on the string and add it to a new String until the char ',' –  Jun 20 '21 at 03:20
  • Right, but strcpy is usually implemented in hand-written asm for performance. And you can find lots of examples of simple (not high performance) implementations of it in asm for any ISA, including 16-bit x86, if you google, or even search on Stack Overflow. – Peter Cordes Jun 20 '21 at 12:10
  • Of course, for this problem, it would probably be easier to just use separate labels for the parts of your string data, so you'd only need a normal print function that loops or uses a DOS `$`-terminated string output function. – Peter Cordes Jun 20 '21 at 12:12
  • @PeterCordes are you sure, i will need to get a separate string from input every single time? –  Jun 20 '21 at 13:54
  • I skimmed too fast and thought you were printing those as a sequence of prompts, not matching them against user input. For that, it would be more efficient to make an array of `pointer, length` that you could search in, especially if you require exact matches of full length. Or a `memmem` / `strstr` type of nested loop, but counting how many `','` characters you pass before a match. You don't actually need to extract to anywhere. – Peter Cordes Jun 21 '21 at 15:47
  • Try `repne scasb` with the desired character in `al` and the pointer to the start of the string in `si` (make sure you set `cx` to zero and `ds` to `@data`). That will return the negative length in `cx` and a pointer to the character you checked for in `si` – puppydrum64 Nov 28 '22 at 17:36

1 Answers1

1
ourbuffer db ?
temp db ?
FirstS db "Please ...

get the string from the user (into ourbuffer)
i want to extract each sentance into temp

The definitions of ourbuffer and temp merily reserve a single byte of memory each! To get a decent buffer use something like db 128 dup (0).
To learn more about the DOS.BufferedInput function 0Ah read How buffered input works.

On the question of copying a string until a specific character.

Next Extract routine will retrieve all the characters until a "," is found or until the DOS terminating carriage return is found. The CX register will tell you how many characters were deposited in the temp buffer. This count does not include the terminating comma although that comma is present in the temp buffer.

; IN (si) OUT (cx,si)
Extract:
  push ax
  mov  cx, di      ; Preserves DI
  mov  di, -1
.More:
  inc  di
  mov  al, ourbuffer[si]
  mov  temp[di], al
  inc  si
  cmp  al, ","
  je   .OK
  cmp  al, 13
  jne  .More
  dec  si
  mov  byte ptr [di], ","  ; Replace 13 by ,
.OK:
  xchg cx, di
  pop  ax
  ret

To extract the first command use:

mov  si, 2      ; The inputted characters start at offset 2 with this DOS function!
call Extract    ; -> CX SI

Now you can compare the contents of temp with the records in your long Table. However, you must preserve the SI index so as to be able to extract any remaining commands from the input in ourbuffer!

To extract following commands keep using:

push si
...       Here goes comparing commands, etc
pop  si
call Extract    ; -> CX SI

If upon returning from Extract CX=0, then you know that there's nothing more to extract from ourbuffer. Beware, this could have happened at the first extraction already...

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Of course, you don't need to actually copy to a temp buffer. It looks like the querent actually wants to match input strings against an array / table of strings, in order to dispatch to the right function. So `strstr` or `memmem` and count how many `',` chars you passed before a match, if we insist on not also storing an array of pointers + lengths to let us loop over the sub-strings. – Peter Cordes Jun 21 '21 at 15:45