为什么我需要在这个汇编游戏中转换数字

问题描述 投票:1回答:1

我需要使用汇编语言来制作猜谜游戏。

游戏要求玩家输入一个数字并将其与设置另一个数字,然后告诉玩家他们的猜测是否太高或太低,让玩家不断猜测,直到他们获取号码集。

该工作的问题之一是:

为什么需要数字转换以及如何–使用网格方法-ASCII H之间的转换从十六进制转换为二进制]

我为什么需要进行转换?

游戏代码

.data #storing data
    startmsg: .asciiz "I am thinking of a random number between 1 and 100\n\n"
    guessmsg: .asciiz "Enter your guess\n"
    tooHigh: .asciiz "Your guess is too high.\n"
    tooLow: .asciiz "\n\nYour guess is too low.\n"
    wingame: .asciiz "You have guessed the number. Well done!\n\n"
.text #start of program

start:
    jal random
    add $t0, $zero, $a0 # store random number $a0 in $t0
    li $v0, 4         # print string
    la $a0, startmsg
    syscall

#######################################
# Main game loop for guessing
guessing:
    la $a0, guessmsg
    li $v0, 4 # print string guessmsg
    syscall




    li $v0, 5 #read int from user
    syscall
        move $t1, $v0 # store input in t1
        #addi $t2, $zero, 1 #t2 = 1 (guess min)
    beq $t0, $t1, win # if stored int = user input, user won
    addi $s0, $s0, -1 # guess used, subtract
    blt $t0, $t1, goLower # if stored int < user input, guess is too high

    # otherwise, guess is too low
    la $a0, tooLow
    li $v0, 4 # print string tooLow
        syscall

    # loop guessing
    j guessing

#######################################
# goLower: Procedure if the user guess too high
goLower:
    la $a0, tooHigh
    li $v0, 4 # print tooHigh
    syscall
    # loop back to get another guess
    j guessing

#######################################
# User won, print win and restart
win:
    la $a0, wingame
    li $v0, 4 #print string wingame
    syscall
    j start


#############################################
# LEAF PROCEDURE
# random: generate a rand number between 0 - 100
random: 
    li $v0, 42        # SERVICE 41 for a rand int
    #addi $a0, $zero, 0 # random number >= 0
    addi $a1, $zero, 100 #random number < 100
    #xor $a0, $a0, $a0  # Select random generator 0
    syscall            # Generate random int (returns in $a0)
    jr $ra
assembly mips
1个回答
0
投票

现在的代码通过使用read_int服务(系统调用代码5)通过以下方式调用它

li $v0, 5 #read int from user
syscall

现在,如果要读取ASCII字符,则应该使用服务read_string(系统调用代码8),该服务将把读取缓冲区存储在$a0中,并将其长度存储在$a1中。

li $v0, 8 # Note the only change is 8 instead of 5
syscall

现在将输入数字与代码中的数字进行比较,您有两个选择:

  • 编写代码,使代码生成的数字集采用ASCII格式,并比较两个ASCII值。
  • 将用户输入的ASCII字符转换为十进制并比较数字。

对于第一个,您需要将数字拆分成数字,然后将它们加48,然后将结果连接成字符串。

对于第二种解决方案,您需要遍历缓冲区并将48减去每个ASCII字符。

[这是将ASCII数组转换为整数的算法,但是要工作,它假定您将缓冲区存储在$s1中,$t0包含10,并且$s2包含0

lp:
  lbu $t1, ($s1)       #load unsigned char from array into t1
  beq $t1, $0, FIN     #NULL terminator found
  blt $t1, 48, error   #check if char is not a digit (ascii<'0')
  bgt $t1, 57, error   #check if char is not a digit (ascii>'9')
  addi $t1, $t1, -48   #converts t1's ascii value to dec value
  mul $s2, $s2, $t0    #sum *= 10
  add $s2, $s2, $t1    #sum += array[s1]-'0'
  addi $s1, $s1, 1     #increment array address
  j lp                 #jump to start of loop
© www.soinside.com 2019 - 2024. All rights reserved.