MIPS : parsing and modifying a stringAccessing one character in a stringCode is reading proceeding string with specified oneMIPS Infinite loop/error with loading char byteMIPS Address out of range (MARS)Printing corresponding ascii value from inputted intsMIPS (Bare Mode) String Won't Print.align directive in MIPS assembly (MARS)MIPS code doesn't print strings loaded from memoryMIPS reverse order of Capital Letters in each word in string

Formal Definition of Dot Product

How do employ ' ("prime") in math mode at the correct depth?

What's the difference between "за ... от" and "в ... от"?

Is this apt vulnerability (CVE-2019-3462) a security concern for Ubuntu users?

Non-deterministic Finite Automata | Sipser Example 1.16

Determine the slope and write the Cartesian equation of the line.

Could there be a material that inverts the colours seen through it?

Is taking modulus on both sides of an equation valid?

Can I say: "When was your train leaving?" if the train leaves in the future?

Will a coyote attack my dog on a leash while I'm on a hiking trail?

Ito`s Lemma problem

Why was Thor doubtful about his worthiness to Mjolnir?

How i can place a block anywhere in Store

Labeling matrices/rectangles and drawing Sigma inside rectangle

Program which behaves differently in/out of a debugger

Missouri raptors have wild hairdos

Do Life Drain attacks from wights stack?

How can I answer high-school writing prompts without sounding weird and fake?

Smallest Guaranteed hash collision cycle length

What information do scammers need to withdraw money from an account?

using `is` operator with value type tuples gives error

Why does lemon juice reduce the "fish" odor of sea food — specifically fish?

Tikz draw contour without some edges, and fill

As programers say: Strive to be lazy



MIPS : parsing and modifying a string


Accessing one character in a stringCode is reading proceeding string with specified oneMIPS Infinite loop/error with loading char byteMIPS Address out of range (MARS)Printing corresponding ascii value from inputted intsMIPS (Bare Mode) String Won't Print.align directive in MIPS assembly (MARS)MIPS code doesn't print strings loaded from memoryMIPS reverse order of Capital Letters in each word in string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have to write a program in MIPS where I have to open a file whose name is entered by the user and parse it,using the MARS simulator .
However, since the syscall 8 (which reads a string from user input) follows the semantics of the C function fgets, I have to remove the line feed n character from the string before I can try and open the file.



I am able to parse the string (here I print the ASCII code of every character for debugging purpose ) .
However when I try to change the line feed character into a NULL character with the line



sb $zero 0($t1)


Mars encounters an exception during execution :




"Runtime exception at 0x00400050: address out of range 0x00000000"




If I comment out this line , the program runs fine and prints every ascii code of the string.



.data

prompt : .asciiz "enter filename:n"
lf : .asciiz "n"
space : .asciiz " "

.text

la $a0 prompt # asking user for filename
jal print_string
jal read_string

la $t0 ($a0) #copying the string address
lb $t1 0($t0) #loading the first byte

parsing_string : #loop
beq $t1 0 remove_line_feed # reaching the NULL character ''

move $a0 $t1 #printing the ascii code
li $v0 1
syscall

la $a0 space #printing a space
li $v0 4
syscall

add $t0 $t0 1 # loading
lbu $t1 ($t0) # the next byte

j parsing_string

remove_line_feed :
sb $zero 0($t1) # runtime exception if i uncomment this line
j end

end :


li $v0 10 # syscall 10 : end program
syscall



read_string : #read a string in $a0

li $a1 100 # $a1 = maximum number of characters to read
li $v0 8 # syscall 8 : fgets()
syscall
jr $ra


print_string : #print string in $a0
li $v0 4 # syscall 4 : print string
syscall
jr $ra


I don't understand what I'm doing wrong here, at this point of the program the register $t1 should contain the address where the line feed character is written .
However I may have misunderstood how the instructions save byte and load byte actually work.



Any help or suggestions are greatly appreciated :)










share|improve this question




























    0















    I have to write a program in MIPS where I have to open a file whose name is entered by the user and parse it,using the MARS simulator .
    However, since the syscall 8 (which reads a string from user input) follows the semantics of the C function fgets, I have to remove the line feed n character from the string before I can try and open the file.



    I am able to parse the string (here I print the ASCII code of every character for debugging purpose ) .
    However when I try to change the line feed character into a NULL character with the line



    sb $zero 0($t1)


    Mars encounters an exception during execution :




    "Runtime exception at 0x00400050: address out of range 0x00000000"




    If I comment out this line , the program runs fine and prints every ascii code of the string.



    .data

    prompt : .asciiz "enter filename:n"
    lf : .asciiz "n"
    space : .asciiz " "

    .text

    la $a0 prompt # asking user for filename
    jal print_string
    jal read_string

    la $t0 ($a0) #copying the string address
    lb $t1 0($t0) #loading the first byte

    parsing_string : #loop
    beq $t1 0 remove_line_feed # reaching the NULL character ''

    move $a0 $t1 #printing the ascii code
    li $v0 1
    syscall

    la $a0 space #printing a space
    li $v0 4
    syscall

    add $t0 $t0 1 # loading
    lbu $t1 ($t0) # the next byte

    j parsing_string

    remove_line_feed :
    sb $zero 0($t1) # runtime exception if i uncomment this line
    j end

    end :


    li $v0 10 # syscall 10 : end program
    syscall



    read_string : #read a string in $a0

    li $a1 100 # $a1 = maximum number of characters to read
    li $v0 8 # syscall 8 : fgets()
    syscall
    jr $ra


    print_string : #print string in $a0
    li $v0 4 # syscall 4 : print string
    syscall
    jr $ra


    I don't understand what I'm doing wrong here, at this point of the program the register $t1 should contain the address where the line feed character is written .
    However I may have misunderstood how the instructions save byte and load byte actually work.



    Any help or suggestions are greatly appreciated :)










    share|improve this question
























      0












      0








      0








      I have to write a program in MIPS where I have to open a file whose name is entered by the user and parse it,using the MARS simulator .
      However, since the syscall 8 (which reads a string from user input) follows the semantics of the C function fgets, I have to remove the line feed n character from the string before I can try and open the file.



      I am able to parse the string (here I print the ASCII code of every character for debugging purpose ) .
      However when I try to change the line feed character into a NULL character with the line



      sb $zero 0($t1)


      Mars encounters an exception during execution :




      "Runtime exception at 0x00400050: address out of range 0x00000000"




      If I comment out this line , the program runs fine and prints every ascii code of the string.



      .data

      prompt : .asciiz "enter filename:n"
      lf : .asciiz "n"
      space : .asciiz " "

      .text

      la $a0 prompt # asking user for filename
      jal print_string
      jal read_string

      la $t0 ($a0) #copying the string address
      lb $t1 0($t0) #loading the first byte

      parsing_string : #loop
      beq $t1 0 remove_line_feed # reaching the NULL character ''

      move $a0 $t1 #printing the ascii code
      li $v0 1
      syscall

      la $a0 space #printing a space
      li $v0 4
      syscall

      add $t0 $t0 1 # loading
      lbu $t1 ($t0) # the next byte

      j parsing_string

      remove_line_feed :
      sb $zero 0($t1) # runtime exception if i uncomment this line
      j end

      end :


      li $v0 10 # syscall 10 : end program
      syscall



      read_string : #read a string in $a0

      li $a1 100 # $a1 = maximum number of characters to read
      li $v0 8 # syscall 8 : fgets()
      syscall
      jr $ra


      print_string : #print string in $a0
      li $v0 4 # syscall 4 : print string
      syscall
      jr $ra


      I don't understand what I'm doing wrong here, at this point of the program the register $t1 should contain the address where the line feed character is written .
      However I may have misunderstood how the instructions save byte and load byte actually work.



      Any help or suggestions are greatly appreciated :)










      share|improve this question














      I have to write a program in MIPS where I have to open a file whose name is entered by the user and parse it,using the MARS simulator .
      However, since the syscall 8 (which reads a string from user input) follows the semantics of the C function fgets, I have to remove the line feed n character from the string before I can try and open the file.



      I am able to parse the string (here I print the ASCII code of every character for debugging purpose ) .
      However when I try to change the line feed character into a NULL character with the line



      sb $zero 0($t1)


      Mars encounters an exception during execution :




      "Runtime exception at 0x00400050: address out of range 0x00000000"




      If I comment out this line , the program runs fine and prints every ascii code of the string.



      .data

      prompt : .asciiz "enter filename:n"
      lf : .asciiz "n"
      space : .asciiz " "

      .text

      la $a0 prompt # asking user for filename
      jal print_string
      jal read_string

      la $t0 ($a0) #copying the string address
      lb $t1 0($t0) #loading the first byte

      parsing_string : #loop
      beq $t1 0 remove_line_feed # reaching the NULL character ''

      move $a0 $t1 #printing the ascii code
      li $v0 1
      syscall

      la $a0 space #printing a space
      li $v0 4
      syscall

      add $t0 $t0 1 # loading
      lbu $t1 ($t0) # the next byte

      j parsing_string

      remove_line_feed :
      sb $zero 0($t1) # runtime exception if i uncomment this line
      j end

      end :


      li $v0 10 # syscall 10 : end program
      syscall



      read_string : #read a string in $a0

      li $a1 100 # $a1 = maximum number of characters to read
      li $v0 8 # syscall 8 : fgets()
      syscall
      jr $ra


      print_string : #print string in $a0
      li $v0 4 # syscall 4 : print string
      syscall
      jr $ra


      I don't understand what I'm doing wrong here, at this point of the program the register $t1 should contain the address where the line feed character is written .
      However I may have misunderstood how the instructions save byte and load byte actually work.



      Any help or suggestions are greatly appreciated :)







      assembly mips mars-simulator






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 13:34









      abu hajarabu hajar

      73




      73






















          1 Answer
          1






          active

          oldest

          votes


















          0














          In the asm code



          sb $zero 0($t1) # runtime exception if i uncomment this line 


          you use $t1 as the adress of the byte you want to clear.



          But it is not.
          You used



          la $t0 ($a0) #copying the string address 


          and all your accesses use $t0.



          At the end of your parsing_string function, you read the char at address $t0 and put it in $t1:



          lbu $t1 ($t0) # the next byte


          then you go back to parsing_string, test if t1 is 0



          beq $t1 0 remove_line_feed # reaching the NULL character ''


          go to remove_line_feed



          remove_line_feed :
          sb $zero 0($t1) # runtime exception if i uncomment this line
          j end


          Where you try to write zero at address 0+$t1 (that you just tested as being equal to zero).



          And the simulator properly tells you that you try to write at address 0 and generates an error.



          If you change your code to



          remove_line_feed :
          sb $zero 0($t0) # runtime exception if i uncomment this line
          j end


          this will remove the runtime error.



          But, it will not make your code correct. What you want to do is to clear the 'n' which is just before the and you code should be



          remove_line_feed :
          sb $zero -1($t0)
          j end


          Alternatively, you can change the comparison and instead of searching the final of the string, search a 10 (ascii line feed) and replace the same char with 0.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314249%2fmips-parsing-and-modifying-a-string%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In the asm code



            sb $zero 0($t1) # runtime exception if i uncomment this line 


            you use $t1 as the adress of the byte you want to clear.



            But it is not.
            You used



            la $t0 ($a0) #copying the string address 


            and all your accesses use $t0.



            At the end of your parsing_string function, you read the char at address $t0 and put it in $t1:



            lbu $t1 ($t0) # the next byte


            then you go back to parsing_string, test if t1 is 0



            beq $t1 0 remove_line_feed # reaching the NULL character ''


            go to remove_line_feed



            remove_line_feed :
            sb $zero 0($t1) # runtime exception if i uncomment this line
            j end


            Where you try to write zero at address 0+$t1 (that you just tested as being equal to zero).



            And the simulator properly tells you that you try to write at address 0 and generates an error.



            If you change your code to



            remove_line_feed :
            sb $zero 0($t0) # runtime exception if i uncomment this line
            j end


            this will remove the runtime error.



            But, it will not make your code correct. What you want to do is to clear the 'n' which is just before the and you code should be



            remove_line_feed :
            sb $zero -1($t0)
            j end


            Alternatively, you can change the comparison and instead of searching the final of the string, search a 10 (ascii line feed) and replace the same char with 0.






            share|improve this answer



























              0














              In the asm code



              sb $zero 0($t1) # runtime exception if i uncomment this line 


              you use $t1 as the adress of the byte you want to clear.



              But it is not.
              You used



              la $t0 ($a0) #copying the string address 


              and all your accesses use $t0.



              At the end of your parsing_string function, you read the char at address $t0 and put it in $t1:



              lbu $t1 ($t0) # the next byte


              then you go back to parsing_string, test if t1 is 0



              beq $t1 0 remove_line_feed # reaching the NULL character ''


              go to remove_line_feed



              remove_line_feed :
              sb $zero 0($t1) # runtime exception if i uncomment this line
              j end


              Where you try to write zero at address 0+$t1 (that you just tested as being equal to zero).



              And the simulator properly tells you that you try to write at address 0 and generates an error.



              If you change your code to



              remove_line_feed :
              sb $zero 0($t0) # runtime exception if i uncomment this line
              j end


              this will remove the runtime error.



              But, it will not make your code correct. What you want to do is to clear the 'n' which is just before the and you code should be



              remove_line_feed :
              sb $zero -1($t0)
              j end


              Alternatively, you can change the comparison and instead of searching the final of the string, search a 10 (ascii line feed) and replace the same char with 0.






              share|improve this answer

























                0












                0








                0







                In the asm code



                sb $zero 0($t1) # runtime exception if i uncomment this line 


                you use $t1 as the adress of the byte you want to clear.



                But it is not.
                You used



                la $t0 ($a0) #copying the string address 


                and all your accesses use $t0.



                At the end of your parsing_string function, you read the char at address $t0 and put it in $t1:



                lbu $t1 ($t0) # the next byte


                then you go back to parsing_string, test if t1 is 0



                beq $t1 0 remove_line_feed # reaching the NULL character ''


                go to remove_line_feed



                remove_line_feed :
                sb $zero 0($t1) # runtime exception if i uncomment this line
                j end


                Where you try to write zero at address 0+$t1 (that you just tested as being equal to zero).



                And the simulator properly tells you that you try to write at address 0 and generates an error.



                If you change your code to



                remove_line_feed :
                sb $zero 0($t0) # runtime exception if i uncomment this line
                j end


                this will remove the runtime error.



                But, it will not make your code correct. What you want to do is to clear the 'n' which is just before the and you code should be



                remove_line_feed :
                sb $zero -1($t0)
                j end


                Alternatively, you can change the comparison and instead of searching the final of the string, search a 10 (ascii line feed) and replace the same char with 0.






                share|improve this answer













                In the asm code



                sb $zero 0($t1) # runtime exception if i uncomment this line 


                you use $t1 as the adress of the byte you want to clear.



                But it is not.
                You used



                la $t0 ($a0) #copying the string address 


                and all your accesses use $t0.



                At the end of your parsing_string function, you read the char at address $t0 and put it in $t1:



                lbu $t1 ($t0) # the next byte


                then you go back to parsing_string, test if t1 is 0



                beq $t1 0 remove_line_feed # reaching the NULL character ''


                go to remove_line_feed



                remove_line_feed :
                sb $zero 0($t1) # runtime exception if i uncomment this line
                j end


                Where you try to write zero at address 0+$t1 (that you just tested as being equal to zero).



                And the simulator properly tells you that you try to write at address 0 and generates an error.



                If you change your code to



                remove_line_feed :
                sb $zero 0($t0) # runtime exception if i uncomment this line
                j end


                this will remove the runtime error.



                But, it will not make your code correct. What you want to do is to clear the 'n' which is just before the and you code should be



                remove_line_feed :
                sb $zero -1($t0)
                j end


                Alternatively, you can change the comparison and instead of searching the final of the string, search a 10 (ascii line feed) and replace the same char with 0.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 22:54









                Alain MerigotAlain Merigot

                4,8832822




                4,8832822





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314249%2fmips-parsing-and-modifying-a-string%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현