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;
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
add a comment |
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
add a comment |
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
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
assembly mips mars-simulator
asked Mar 23 at 13:34
abu hajarabu hajar
73
73
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 23 at 22:54
Alain MerigotAlain Merigot
4,8832822
4,8832822
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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