長い時間のlurker、初めてのasker。私はMIPSを使い始めるばかりで、プロジェクトによってちょっと混乱しています。割り当ては、整数入力を受け取り、基数2〜16のアスキー変換を出力するプログラムを作成することです。以下に示すように、私は初期変換を計算しましたが、基数ループはまだ私を回避していると思います。どんな助けでも大歓迎です! EDITitoaさまざまな基数を持つAsciiの整数
.data
str: .space 128 # space for the output string
# examples of macros, for printing.
.macro print_int (%x)
add $a0, $zero, %x
li $v0, 1
syscall
.end_macro
.macro print_str (%x)
la $a0, %x
li $v0, 4
syscall
.end_macro
# str address is in a register
.macro print_str_r (%x)
add $a0, $zero, %x
li $v0, 4
syscall
.end_macro
.text # Code segment
.globl main # declare main to be global
main:
print_str(name) # use macro
# You can read the number with a system call
#li $s0, 49
#li $v0, 5 #read an integer
#syscall
#move $s0, $v0 #save the user input
li $s0, 0xFFFFFFFF # pseudo instruction for loading large constant
la $s1, str
# You need to construct a loop to use radices 2 .. 16
# for $s2 = 2, $s2 < 17, $s2 ++
li $s2, 2
#call itoa
move $a0, $s0 #save n to $s0
move $a1, $s1
move $a2, $s2
jal itoa
# print the radix
print_int($s2) # note $a0 and $v0 are overwritten
print_str(msg_tab)
print_str_r($s1)
print_str(msg_nl)
# Need to jump back to the beginning of the loop
Exit: li $v0,10 # System call, type 10, standard exit
syscall # ...and call the OS
# function itoa (value, buffer, radix)
# it only returns '0' for now.
# you can return an empty string '' if the radix is not supported.
itoa:
addi $t0,$zero,10 # t0=10
addi $t1,$t1,a0 # t1=a0
Loop:
div $t1,$t0 #t1/10
mflo $t1 #t1 = quotient
mfhi $t2 #t2 =remainder
addi $t2,$t2,0x30 #Convert to ASCII
addi $sp,$sp,-1 #Make space for 1 byte in the stack
sb $t2,0($sp) #Push t2 in the stack
addi $v0,$v0,1 #v0=v0+1
bne $t1,$zero,Loop #If t1<>0 go to Loop
order:
sw $t0,$v0 #t0=v0
lb $t1,0($sp) #pop the last byte for the stack
addi $sp,$sp,1 #Reduce the stack size by 1 byte
add $t2,$v0,-$t0 #t2=v0-t0
sb $t1,$t2($a1) # savebyte to the proper location of memory
addi $t0,$t0,-1 #t0=t0-1
bne $to,$zero,order #If t0<>0 go to order
sb 0x0,$v($a0) # add null character to the end of the string
jr $ra
:ここに は、私はちょうど基数のためのループが動作するように変換する必要がなく、作るために方法がわからないよ私が持っているいくつかのCコードです
#include <stdio.h>
#include <stdlib.h>
char * my_itoa(unsigned int v, char *p, int r)
{
unsigned int c;
char *p_old, *q;
static char hexdigits[32] = "ABCDEF";
if (r < 2 || r > 16) {
*p = 0;
return p;
}
if (v == 0) { // return '0'
p[0] = '0';
p[1] = 0; // end of the string.
return p;
}
p_old = p; // save the starting address of the buffer
// doing the conversion
// p points to the location where to store the next character
while (v > 0) {
// You can get both c an v with ONE MIPS instruction
c = v % r;
v = v/r;
*p = hexdigits[c];
p ++; // increment p
}
*p = 0; // end of the string
// reverse the string
// q points to the head and p points to the tail
q = p_old;
p = p - 1;
while (q < p) {
// swap *q and *p
c = *q;
*q = *p;
*p = c;
// increment q and decrement p
q ++;
p --;
}
return p_old;
}
char buf[128];
int main (int argc, char **argv)
{
int r;
unsigned int n = (argc > 1) ? atoi(argv[1]) : (unsigned int)-1;
for (r = 2; r <= 16; r ++)
printf("r=%d\t%s\n", r, my_itoa(n, buf, r));
return 0;
}