how to convert simple string cpp variable to MIPS code - c++

Sounds simple, but I have been searching for a good hour or so now.
I want to know if there is a way for me to start a register with a string without having to store the string in .data.
For example, I can do; li $t0, 0 which has the effect of setting $t0 = 0. This is the same as me saying int a = 0 in cpp.
However, is there a similar way for me to convert std::string a = "Hello" to MIPS?
Currently I can do something like this
.data
mes: .asciiz "Hello" #declares 'mes' as "Hello"
.text
la $t0, mes #sets $t0 equals to the content of 'mes'
but I wish I could skip the first step by doing something like
sc $t0, "Hello" #set $t0 to characters "Hello" (note: this is not valid MIPS)

A string is nothing but a pointer to a byte/char that ends at 0 value byte.
Since there is no native c++ to MIPS compiler we could argue with common sense that the simplest way to define and initialize a string in MIPS is
.data
mes: .asciiz "Hello"
.text
la $t0, mes

Related

a function compare 2 string in mips

this function should simply verify if the input is equal to the string simolo1
even if I input exactly the same string the output is strings are not equal.
I am saving in the registers s2 and s3 the pointers to the arrays. then I compare each byte, if all of those are equal the program should finish and output 'strings are equal'
.data
prompt: .asciiz "Enter string ('.' to end) > "
dot: .asciiz "."
eqmsg: .asciiz "strings are equal\n"
nemsg: .asciiz "strings are not equal\n"
str1: .space 80
str2: .space 80
simbolo1: .asciiz "sasso"
simbolo2: .asciiz "carta"
simbolo3: .asciiz "forbice"
.text
.globl main
main:
# get first string
la $s2,str1
move $t2,$s2
jal getstr
la $s3, simbolo1
# string compare loop (just like strcmp)
cmploop:
lb $t2,($s2) # get next char from str1
lb $t3,($s3) # get next char from str2
bne $t2,$t3,cmpne # are they different? if yes, fly
beq $t2,$zero,cmpeq # at EOS? yes, fly (strings equal)
addi $s2,$s2,1 # point to next char
addi $s3,$s3,1 # point to next char
j cmploop
# strings are _not_ equal -- send message
cmpne:
la $a0,nemsg
li $v0,4
syscall
j main
# strings _are_ equal -- send message
cmpeq:
la $a0,eqmsg
li $v0,4
syscall
j main
# getstr -- prompt and read string from user
#
# arguments:
# t2 -- address of string buffer
getstr:
# prompt the user
la $a0,prompt
li $v0,4
syscall
# read in the string
move $a0,$t2
li $a1,79
li $v0,8
syscall
# should we stop?
la $a0,dot # get address of dot string
lb $a0,($a0) # get the dot value
lb $t2,($t2) # get first char of user string
beq $t2,$a0,exit # equal? yes, exit program
jr $ra # return
# exit program
exit:
li $v0,10
syscall

Break anywhere when $r1 equals to a certain string in GDB

Is there anyway to break on gdb when $r1 equals to a certain string I know there is :
break [addr] if ((int)strcmp($r1, "hello")) == 0
But what to set in addr when I just wanna break when the r1 is "hello" no matter the current function/address?

Ruby - Unable to count the array values?

I have a program :
Question : Input a number of integer of 2 digit only , and in the out-put it should show the all input values BUT loop should stop on 42 :
example
input
1
2
87
42
99
output
1
2
87
my code
a = []
5.times do |i|
a[i] = Integer(gets.chomp)
end
a.each do |e|
break if e == '42'
puts e
end
Few things to change. First of all gets will give you a string together with \n at the end, so you need to change it to gets.chomp to remove it.
Now your loop should look like this:
a.each do |e|
break if e == '42'
puts e
end
However ruby's array has much butter function which is perfect for what you want:
puts a.take_while {|e| e != '42'}
Additional notes:
Note that it is operating on strings rather than numbers. You might need to validate the input at some point and convert it into integer values.
5.times do|i| - the |i| bit is obsolete.

how to replace a value of a number in a text file with another using fortran

I have a text file in which I want to replace my temperature values. The position of this numbers are random in the text and looks like:
'xdd "D:\Data\Ioana\DH1_Short_slow\DH1_360_2.xye" xye_format
local !Temperature 360
... (more text)
prm bnonh360 1.66237`_0.41541
prm bh360 = 1.2 * bnonh360;
site Na1 x ref_flag -0.11868`_0.00258 y ref_flag 0.51229`_0.00446 z ref_flag 0.00330`_0.00107 occ Na 1 beq = bnonh360;
...(more text)
Out_CIF_STR_Uiso("D:\Data\Ioana\DH1_Short_slow\DH1_360.cif")
Out_Profile("D:\Data\Ioana\DH1_Short_slow\DH1_360_plot.pro")
Out_Tick("D:\Data\Ioana\DH1_Short_slow\DH1_360_plot.tic") '
More text means more text in the file. I want to replace 360 with 365 for instance.
I have tried something like
do
read(10,'(a)',iostat=iok) line1
found = ( (index(line1,'360') /=0) )
if (found) then
write(*,*) '365'
endif
if(line1 == '$') exit
write(*,*) line1
write(40,*,iostat=iok) line1
enddo
But this just writes 365 in a next line where 360 is found.
Thanks
you are discarding valuable information in the return value of index intrinsic. to replace '360' to '365', which are of equal length, you can do
pos=index(line1,'360')
if(pos>0) line1(pos:pos+2)='365'
or in general if you want to replace first occurrence of substring s1 with s2 in a string line, with s1 and s2 possibly having different length, and that they are padded with an indefinite number of spaces that you did not want to include in the replacement, you do
pos=index(line,trim(s1))
if(pos>0) line=line(1:pos-1)//trim(s2)//line(pos+len_trim(s1):len_trim(line))
Should you want to include padding space in the replacement you can change the position offsets in the RHS substrings. If you want to perform replacements for all occurrences you can enclose this in a while loop
This is of course under the assumption that the declared length of line is long enough to hold the new string. However, note that self-assignment is prohibited in FORTRAN77 so if that is what you are using, you will need different string variable to store the result.

Print multiple variables with one command in GDB

I want to execute the very simple command
print var1, var2, var3, var4
in gdb to examine the values of the vars from time to time.
I don't want to use display because it clutters up my view.
How can I do this? Right now all I can do is:
p var1
p var2
p var3
p var4
You can simply do this
print {var1,var2,var3,var4}
This will do the job.
Use the printf command. It's a bit of a hassle, but it gives good control over the formatting. From the command line:
(gdb) help printf
printf "printf format string", arg1, arg2, arg3, ..., argn
This is useful for formatted output in user-defined commands.
The format string is like in C (%d for normal size ints, %s for null terminated strings, etc.).
Use Macros:
For example to continue to next break point and print
(gdb) define prm
Type commands for definition of prm.
End with a line saying just end.
>continue
>print var1
>print var2
>print var3
>end
(gdb) prm
$5 = 0
$6 = 10
$7 = -1
There may be a simpler solution, but you might be able to put together something using GDB macros: http://www.ibm.com/developerworks/aix/library/au-gdb.html