打印用户使用syscall 3和syscall 5输入的值EduMIPS64

时间:2019-01-23 19:25:17

标签: mips

我试图从用户那里读取输入并进行打印。 首先,我向用户打印请求,用户输入一个值,然后我想打印它。

                            .data                           
params_sys5:                .space      8
params_sys3:                .space      8

prompt_msg_LBound:          .asciiz     "Enter lower bound for x,y\n"
prompt_msg_LBound_val:      .asciiz     "Lower bound for x,y = %d\n"
xyL:                        .word64     0
prompt_msg_UBound:          .asciiz     "Enter upper bound for x,y\n"
prompt_msg_UBound_val:      .asciiz     "Upper bound for x,y = %d\n"
xyU:                        .word64     0
prompt_msg_UBoundZ:         .asciiz     "Enter upper bound for z\n"
prompt_msg_UBoundZ_val:     .asciiz     "Lower bound for z = %d\n"
zU:                         .word64     0

prompt_msgAns:              .asciiz     "x = %d, y = %d, z = %d\n"
                            .word64     0
                            .word64     0
                            .word64     0

xyL_Len:                    .word64     0
xyU_Len:                    .word64     0
zU_Len:                     .word64     0

xyL_text:                   .space      32  
xyU_text:                   .space      32  
zU_text:                    .space      32

ZeroCode:                   .word64     0x30    ;Ascii '0'

                            .text
main:                       daddi       r4, r0, prompt_msg_LBound
                            jal         print_string

                            daddi       r8, r0, xyL_text    ;r8 = xyL_text
                            daddi       r14, r0, params_sys3 
                            daddi       r9, r0, 32  
                            jal         read_keyboard_input
                            sd          r1, xyL_Len(r0) ;save first number length

                            ld          r10, xyL_Len(r0)    ;n = r10 = length of xyL_text
                            daddi       r17, r0, xyL_text
                            jal         convert_string_to_integer   ;r17 = &source string,r10 = string length,returns computed number in r11

                            sd          r11, xyL(r0)
                            daddi       r4, r0, prompt_msg_LBound_val
                            jal         print_string                            

end:                        syscall     0

print_string:               sw          $a0, params_sys5(r0) 
                            daddi       r14, r0, params_sys5
                            syscall     5
                            jr          r31


read_keyboard_input:        sd          r0, 0(r14)  ;read from keyboard
                            sd          r8, 8(r14)  ;destination address
                            sd          r9, 16(r14) ;destination size
                            syscall     3
                            jr          r31

convert_string_to_integer:  daddi       r13, r0, 1  ;r13 = constant 1
                            daddi       r20, r0, 10 ;r20 = constant 10
                            movz        r11, r0, r0 ;x1 = r11 = 0
                            ld          r19, ZeroCode(r0)                               
For1:                       beq         r10, r0, EndFor1    
                            dmultu      r11, r20    ;lo = x * 10
                            mflo        r11 ;x = r11 = lo = r11 * 10
                            movz        r16, r0, r0 ;r16 = 0
                            lbu         r16, 0(r17) ;r16 = text[i]
                            dsub        r16, r16, r19   ;r16 = text[i] - '0'
                            dadd        r11, r11, r16   ;x = x + text[i] - '0'
                            dsub        r10, r10, r13   ;n--
                            dadd        r17, r17, r13   ;i++
                            b           For1
EndFor1:                    jr          r31             

我正在尝试获取第一个数字,即x,y的下限。 例如,我键入数字5,所以最后xyL表示为5,但打印的字符串为:

Enter lower bound for x,y Lower bound for x,y = 0

如何打印输入的值,然后对下一个字符串进行打印?

谢谢。

编辑:========================================= =========================== 我通过添加另一种数据类型.data来保存地址,从而更改了.space 8,现在,我调用print_string而不是跳转到syscall 5来打印值,例如: / p>

prompt_msg_LBound:          .asciiz     "Enter lower bound for x,y\n"
prompt_msg_LBound_val:      .asciiz     "Lower bound for x,y = %d\n"
LBound_val_addr:            .space      8                       
xyL:                        .space      8

以及.code部分:

                            sd          r11, xyL(r0)                                
                            daddi       r5, r0, prompt_msg_LBound_val
                            sd          r5, LBound_val_addr(r0)                                                             
                            daddi       r14 ,r0, LBound_val_addr
                            syscall 5

但是我仍然想使用print_string来打印带有用户输入值的字符串:prompt_msg_LBound_val

我该怎么做?

1 个答案:

答案 0 :(得分:1)

手册中的print_string示例函数并不用于占位符,只能用于纯字符串。

如果将占位符添加到格式字符串,则SYSCALL 5将继续从内存中读取那些占位符的值。在这种情况下,它只读取并显示值0,这是偶然的内存中的值。

请参见手册中的printf()示例(稍作更新和注释)以检查如何使用占位符:

                .data
format_str:     .asciiz   "%dth of %s:\n%s version %i.%i.%i is being tested!"
s1:             .asciiz   "February"
s2:             .asciiz   "EduMIPS64"
fs_addr:        .space    4         ; Will store the address of the format string
                .word     10        ; The literal value 10.
s1_addr:        .space    4         ; Will store the address of the string "February"
s2_addr:        .space    4         ; Will store the address of the string "EduMIPS64"
                .word     1         ; The literal value 1.
                .word     2         ; The literal value 2.
                .word     6         ; The literal value 6.
test:
                .code
                daddi     r5, r0, format_str
                sw        r5, fs_addr(r0)
                daddi     r2, r0, s1
                daddi     r3, r0, s2
                sd        r2, s1_addr(r0)
                sd        r3, s2_addr(r0)
                daddi     r14, r0, fs_addr
                syscall   5
                syscall   0
相关问题