flat assembler
Message board for the users of flat assembler.

Index > DOS > Still can't output string after modification, help with C...

Author
Thread Post new topic Reply to topic
asdf



Joined: 19 Jun 2007
Posts: 1
asdf 19 Jun 2007, 20:34
Title: Still can't output string after modification, help with C++ conversion?

I have been trying very hard to input a string (cin>>arrayOrCPPString or getline(cin, ...) increase each character by one, and output the result.

In C++, the code:

Code:
#include <iostream>
#include <string>  // For string usage
using namespace std;

int main()
{
   char* output="";  // Initialize and declare a variable of type pointer to char
   cout<<"Please enter a string to be transformed: "   // Echo request for input
   getline(cin, output, '\n');
   cout<<"\n";  // Nice formatting
   for(int i=0; i<strlen(output); i++)  // For loop executes for the number of times as the length of output
   {
      output[i]+=1;   // Increase output[i] by one (output[i]++; would be identical) 
   }
   cout<<"Result is: " // Echo "Result is:"
   <<output  // Show transformed output
   <<"\n";  // Nice output/formatting (<<endl; would be identical)
}
    


I have read the documentation and is still very confused of what to do after several hours of practice.

I'd really like some help!

Can this be converted to assembly? I would greatly appreciate it!

Thanks in advance.
Post 19 Jun 2007, 20:34
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 20 Jun 2007, 02:45
Do you mean like this ? :
Code:
; Dos input string +1 demo,  by Dex.
ORG 100h
use16        
        mov   si,message1                   ; Point to first message
        call  print                         ; Call print function
        call  GetCommand                    ; Call our get sting function
        mov   si,message2                   ; Point to second message
        call  print                         ; Call print function
        mov   si,CommandBuffer              ; Point to command buffer 
        call  print                         ; Call print function
        mov   si,message3                   ; Point to third message
        call  print                         ; Call print function
        xor   ax,ax
        int   16h                           ; Call wait for keypress
         mov   ax,4C00h
      int   21h                           ; Exit to Dos
;====================================================;
; GetCommand                                         ;
;====================================================;
GetCommand:
        pusha                               ; Save genral regs
        push  es                            ; Save ES
        push  cs                            ; Move CS to stack
        pop   es                            ; Move CS from stack in to ES
        mov   di,CommandBuffer              ; Move the address of CommandBuffer in DI
       mov   cx,64                         ; move 64 in to counter
 mov   dx,di                         ; Remember initial offset
getkey:
        xor   ax,ax                         ; Get a key
     int   16h                           ; Call interrupt service
        cmp   al,13                         ; Enter?
        je    AH_3hd                        ; Jump = to label AH_3hd
        cmp   al,8                          ; Backspace?
    je    backspace                     ; Jump = to label backspace
     mov   bx,di                         ; Are we at CX bytes already?
   sub   bx,dx
 cmp   bx,cx
 jae   getkey                        ; Jump above or = to label getkey
       mov   ah,0Eh                        ; Write Character
       mov   bx,0x0001
     int   10h                           ; Call interrupt service
        add   al,1                          ; Char + 1
      stosb                               ; Record and display keystroke
  jmp   getkey                        ; Jump to label getkey
backspace:
        cmp   di,CommandBuffer              ; Compear pointer to start of buffer
        jbe   getkey                            ; Jump bellow or = to label getkey
      dec   di                            ; Go back in buffer
     mov   al,8                          ; Move cursor back
      mov   ah,0Eh                        ; Request display
       mov   bx,0x0001
     int   10h                           ; Call interrupt service
        mov   al,32                         ; Print a space
 mov   ah,0Eh                        ; Request display
       mov   bx,0x0001
     int   10h                           ; Call interrupt service
        mov   al,8                          ; Move cursor back again
        mov   ah,0Eh                        ; Request display
       mov   bx,0x0001
     int   10h                           ; Call interrupt service
        jmp   getkey                        ; Get another key
AH_3hd:                                        ; Finish up
     mov   cx,di                         ; CX = byte count
       sub   cx,dx
 xor   al,al                         ; Zero-terminate
        stosb                               ; Store a byte
  xor   ax,ax                         ; Success
       mov   si,CommandBuffer              ; Move the address of CommandBuffer in SI
       mov   ax,si                         ; Move SI into AX
       mov   dx,si                         ; Move SI into DX
        pop   es                            ; Restore ES
        popa                                ; Restore genral regs
      ret                                 ; Return

;====================================================;
;  print.                                            ;
;====================================================;
print:
        mov   ah,0Eh                       ; Request display
again1:
 lodsb                              ; load a byte into AL from DS:SI
        or   al,al                         ; Or AL
        jz   done1                         ; Jump 0, to label done1
        int  10h                           ; Call interrupt service
        jmp  again1                        ; Jump to label again1
done1:
      ret                                ; Return

message1: db "Please enter a string to be transformed: ",10,13,0
message2: db 10,13, "Result is: ",0
message3: db 10,13, "Press anykey to exit. ",0
CommandBuffer: times  128  db 0
    
Post 20 Jun 2007, 02:45
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 20 Jun 2007, 03:17
Dex, it is not really the same. You're using BIOS ints instead of DOS ints. Normally not a problem but if you want to use redirected both stdin and/or stdout, using BIOS ints will override the redirection while the C++ program will not.
Post 20 Jun 2007, 03:17
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 20 Jun 2007, 05:02
LocoDelAssembly wrote:
Dex, it is not really the same. You're using BIOS ints instead of DOS ints. Normally not a problem but if you want to use redirected both stdin and/or stdout, using BIOS ints will override the redirection while the C++ program will not.

Thanks for pointing that out LocoDelAssembly, but i am use to code without OS support Wink.
Post 20 Jun 2007, 05:02
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 20 Jun 2007, 15:32
asdf wrote:
char* output="";
getline(cin, output, '\n');
1)You can't write to a string constant.
2)If you could, the one you're using isn't big enough to hold a line of text anyway.
3)The syntax of the getline that writes to plain char arrays is stream.getline(buffer, maxsize, delimiter). There is also a getline(stream, string, delimiter) function which is for std::string objects - don't get the two confused.

This should work:
char output[80];
cin.getline(output, 80, '\n');
Quote:
for(int i=0; i<strlen(output); i++)
This works but it's bad programming practice, since it calls strlen (a function which has to scan the entire string) again for every character, so it's slow for long strings. When you want to see if you're at the end of a nul-terminated string, just check for the nul-terminator yourself:
for(int i=0; output[i] != '\0'; i++)
Post 20 Jun 2007, 15:32
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.