如何从MIPS中的字符串中获取指定数量的字符? [英] How to get specified number of character from a string in MIPS?

查看:556
本文介绍了如何从MIPS中的字符串中获取指定数量的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

00111100000000010001000000000001
00110100001001000000000000000000
00100100000000100000000000000100
00000000000000000000000000001100
00100100000000100000000000001010
00000000000000000000000000001100

鉴于此机器代码,我只需要获取第一行(32)字符,以便可以将其转换回MIPS指令.我在弄清楚如何获取第一行代码并将其存储到寄存器中时遇到了麻烦.是否有人对如何获得第一行以及此后的下一行有任何想法?系统提示我尝试使用sll或srl指令,但是当我这样做时,我看不到代码中的任何更改.

Given this machine code, I need to just get the first line (32) characters, so that I can convert it back into MIPS instructions. I am having trouble figuring out how to get the first line of code and store it into a register. Does anyone have any ideas on how to get the first line, and the next lines after that? I was given a hint to try using the sll or srl instructions, but when I do that, i do not see any change in the code.

#File to be read from
file: .asciiz "bincode1.txt"
#place to store contents of file
buffer: .space 1024

    .text

#Open the file
li $v0, 13  #system call to open the file
la $a0, file    #load address of the file and store in a0
li $a1, 0   #a1 is reserved for a line in the text file
li $a2, 0
syscall     #opens the file
move $s0, $v0   #save file descriptor 

#Read the file
li $v0, 14  #system call to read from the file
move $a0, $s0   #put the file name into a0
la $a1, buffer  #address of the buffer to read
li $a2, 1024
syscall

#Close the file
li $v0, 16  #system call to close the file
move $a0, $s0   #file name to close
syscall

#print contents of buffer to verify it copied correctly 
li $v0, 4
la $a0, buffer
syscall

#la $t1, buffer
srl $t2, $a0, 32

li $v0, 4
la $a1, ($t2)
syscall

文件bincode1.txt具有前6行机器代码.如果尝试将其偏移32,则会收到错误消息,并且我知道这是不正确的,但是我不确定从这里开始该怎么做.任何建议都会有所帮助.

The file bincode1.txt has the previous 6 lines of machine code. I get an error if I try to shift it by 32, and I know this is incorrect, but I am not sure what to do from here. Any suggestions would be helpful.

推荐答案

在打印内容进行验证后放置一个断点,打开内存视图以指向地址buffer,您将看到文件的内容被读取为连续字节值,这些值经过ASCII编码以形成文本.

Put a breakpoint after printing content for verification, open the memory view to point at address buffer, you will see the content of file was read as consecutive byte values, which are ASCII encoded to form a text.

所以实际的字节值是:

buffer: .byte 48, 48, 49, 49, 49, ... , 48, 49, <EOL>, 48, 48, ...`

<EOL>是行尾(在MIPS上可能是13,也许只是10或两者都在),并且48是ASCII字符'0'的值,而49是ASCII字符'1'.

Where <EOL> is end of line (probably 13 on MIPS, maybe just 10 or both), and 48 is value of ASCII character '0' and 49 is value of ASCII character '1'.

在某些十六进制查看器中查看"bincode1.txt"文件的内容时,您可以看到相同的字节.

You can see the same bytes when you view the content of "bincode1.txt" file in some hexa-viewer.

因此要将32个ASCII字符转换为单个32b值,您必须循环访问32个字节,根据读取的字符('0' (48)/'1' (49),将单个位设置为0/1,决定如何处理非输入错误). -0/1位,为简单起见,您可以执行and char,1从任意字节(甚至是非数字位)中获取0/1位,并忽略不正确的文件),然后将这些位合并在一起.

So to convert 32 ASCII characters into single 32b value you have to loop through 32 bytes, set single bit to 0/1 according to the character read ('0' (48)/'1' (49), decide how to handle wrong input with non-0/1 digit, for simplicity you may just do and char,1 to get 0/1 bit from any byte, even non-digit one, and ignore incorrect files), and merge those bits together.

将位合并在一起的最简单方法是,IMO在循环t0 = 0之前将一些临时寄存器设置为零,然后从buffer读取下一个字符后,首先将其所有位向左移一个sll $t0, $t0, 1,然后然后使用从缓冲区读取的值设置最低有效位(按位OR可能会有所帮助,在此后and char,1从char中仅提取最低(最右边)位).循环运行32次,瞧,您在t0中有32b的数字(当您向左移动时,第一个char读取将在最左位结束,这很可能是您想要的).

The easiest way to merge bits together is IMO to set some temporary register to zero ahead of loop t0 = 0, then after reading next character from buffer you first shift all its bits to left by one sll $t0, $t0, 1, and then set the least significant bit with the one read from buffer (bitwise OR may be helpful, after that and char,1 to extract only lowest (right-most) bit from char). Loop 32 times through this, and voila, you have 32b number in t0 (as you are shifting left, the first char read will end in the left-most bit, which is very likely what you wanted).

然后您可以将其打印为数字值,以验证转换产生了正确的结果.

Then you can print it as number value, to verify the conversion produced correct result.

提示:尝试可视化您的输入和输出数据,以及这些位之间的相关性,与调试器一起玩一下,看看它如何显示内存和寄存器值(也许它允许几种不同的格式/分组,这样可以使您在不同情况下更轻松地检查特定值,例如在检查ASCII数据时通常最好看到单独的字节,但是例如,将4个字节分组为单个值时,最好查看word大小的数字) .然后尝试想象如何处理输入数据以计算预期的输出,并为其编写算法.

Hint: try to visualize your input and output data, and how those bits are relevant to each other, play a bit with debugger to see how it can display the memory and register values (maybe it allows several different formats/grouping, which can make it easier for you to check particular values in different situations, like when checking ASCII data it's usually good to see separate bytes, but for example word sized numbers are better to be viewed when 4 bytes are grouped into single value). Then try to imagine how you would treat the input data to calculate the expected output, and write the algorithm for it.

这篇关于如何从MIPS中的字符串中获取指定数量的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆