Caesar cipher encrypting a single character in MIPS

后端 未结 1 839
名媛妹妹
名媛妹妹 2021-01-26 04:56

I\'m having some problems in creating a program to encrypt a message. At this point i\'m just trying to input a char and the output should be the char+5 positions in the alphabe

相关标签:
1条回答
  • 2021-01-26 05:25

    If you are using MIPS, the code should be like this:

        .text
    main:
        li $v0, 8                 # read_string
        la $a0, textbuf           # adresses of char at $a0
        li $a1, 2                 # length to read at $a1
        syscall
    
        la $t0, textbuf
        lb $v0, 0($t0)            # char ASCII (I GUESS IT SHOULD)
        move $t0, $v0             # moves char(ASCII) to $t0
        li $s0, 90
        li $s1, 65
        bgt $t0, $s0, le_string   # checks if char(ASCII) > 90
        nop                       # avoid instruction after branch begin executed even if jump is taken
        blt $t0, $s1, le_string   # checks if char(ASCII) < 65
        nop                       # avoid instruction after branch begin executed even if jump is taken
    
        li $s2, 5
        li $s3, 26
        add $t0, $t0, $s2         # char=char+5
        ble $t0, $s0, nowrap_char # checks if encrypted char <= 90
        nop                       # avoid instruction after branch begin executed even if jump is taken
        sub $t0, $t0, $s3         # wrap the char
    nowrap_char:
    le_string:
        move $t2, $t0             # moves encrypted char to $t2
    
        la $t0, textbuf
        li $v0, 4                 # print_string
        sb $t2, 0($t0)            # put the encrypted char
        la $a0, textbuf
        syscall
    
        li $v0, 10                # exit
        syscall
    
        .data
    textbuf:
        .space 2
    
    0 讨论(0)
提交回复
热议问题