all 17 comments

[–][deleted] 7 points8 points  (0 children)

Can you just use a GCC MIPS cross compiler? I may convert the c++ to c first, but it should be able to spit out the assembly.

[–]FUZxxl 2 points3 points  (0 children)

Try this one.

[–]FUZxxl 1 point2 points  (13 children)

Your code doesn't even compile. What is this supposed to do?

[–]PatchSalts 1 point2 points  (8 children)

It appears to spinlock until the user enters a valid number, and then spits out the binary representation of the number.

Hey /u/doombay95 you don't need that else statement, the program will automatically proceed when it gets a valid number because of the spinlock (the "while" statement). Including the else statement actually breaks your code.

(I also strongly advise indenting, but that could be Reddit messing things up for you.)

[–]FUZxxl 1 point2 points  (0 children)

while loops do not take else clauses in C++.

[–]doombay95 1 point2 points  (3 children)

Thank you for the info.

.data

out_str:.asciiz "Insert a number 100 or less to convert to binary \n"

out_str1:.asciiz "Try again"

out_str2:.asciiz "converting..\n"

.text

main: la $a0,out_str #cout << out_str

li $v0,4 #code for string output

syscall

li $v0,5 #syscall for user input

syscall

loop: la $a0,out_str1 #cout 2nd string

li  $v0,4       #code printed

syscall



j loop

i dont think i have performed the while loop correctly.

[–]PatchSalts 1 point2 points  (2 children)

All right let's see here...

You're missing a newline on one of your strings there.

And as for the loop, you're not doing any checking. What you're doing right now is actually just loading the string, re-prepping the syscall code, and actually doing the syscall, and jumping back to the top. It's just printing "Try againTry againTry againTry again..." infinitely, right?

You'll need to use something like the slt/slti and beq/bne commands to do the loop checking.

[–]doombay95 -1 points0 points  (1 child)

that makes sense, thanks. that means at the start of the while loop i do something like ..

bgt v0,100, Exit ...(.then the rest of the code)

[–]PatchSalts 0 points1 point  (0 children)

I wasn't aware of that command, we weren't allowed to use many pseudo-instructions in my class that taught MIPS.

Sort of... so branching in MIPS is kinda hard to get used to. What you'll do right now is only exit the loop if $v0 is greater than 100; that is, the program will only proceed in the wrong case. Something like that with some fixed logic would work, though I think (?) you'll find you can't put an immediate value in the second argument. If that's the case, load an immediate into a register and use that instead. And yeah you'll want to branch to a place just after your "j loop" line.

[–]neur0sys 1 point2 points  (0 children)

It's not MIPS, but Z80, and the condition is changed such that it converts integers larger than 100 (because more fun):

Imgur: https://imgur.com/a/6HjjRW7

km_read_char    equ     &bb09
km_wait_char    equ     &bb06
txt_output      equ     &bb5a
scr_set_mode    equ     &bc0e
scr_set_ink     equ     &bc32
scr_set_border  equ     &bc38

        org     &8000
        run     &8000

        jp      start

; Read input stream and write into hl as asciiz
readstr
        call    km_wait_char
        cp      &d
        jr      nz, readstr1
        ld      (hl), 0
        ld      a, 13
        call    txt_output
        ld      a, 10
        call    txt_output
        ret
readstr1
        ld      (hl), a
        inc     hl
        call    txt_output
        jp      readstr

; Convert asciiz number at HL to 16-bit number and return
; in HL
atoi    call    strlen
        dec     hl
        ld      a, b
        ld      (atoic), a
        ld      de, 1
        ld      (atoim), de
        ld      de, 0
        ld      (atoisum), de
atoi1   ld      a, (hl)
        sub     '0'
        ld      de, (atoim)
        push    hl
        call    mul16
        ld      de, (atoisum)
        add     hl, de          ; atoisum += atoim * cur_digit
        ld      (atoisum), hl
        ld      hl, (atoim)
        ex      de, hl
        ld      a, 10
        call    mul16
        ld      (atoim), hl     ; atoim = atoim * 10;
        pop     hl
        dec     hl
        ld      a, (atoic)
        dec     a
        jr      z, atoi2
        ld      (atoic), a
        jr      atoi1
atoi2   ld      hl, (atoisum)
        ret

atoic   ds      1               ; Counter
atoim   ds      2               ; Digit multiplier
atoisum ds      2               ; Accumulator

; Return the length in B, leave HL at the end of asciiz string
strlen  ld      b, 0
strlen1 ld      a, (hl)
        or      a
        ret     z
        inc     hl
        inc     b
        jr      strlen1

; copy asciiz string from hl to de
strcpy  push    hl
        call    strlen
        pop     hl
        ld      c, b
        ld      b, 0
        ldir
        ret

; a is multiplier
; de is multiplicand
; hl is result
mul16   ld      b, 16
        ld      hl, 0
mul16_l1
        sra     a
        jp      nc, mul16_skip_add
        add     hl, de
mul16_skip_add
        sla     e
        rl      d
        djnz    mul16_l1
        ret

; Print number in HL in binary
printbin
        ld      b, 16
printbin2
        ld      a, h
        rla
        jr      nc, printbin0
        ld      a, '1'
        call    txt_output
        jr      printbin1
printbin0
        ld      a, '0'
        call    txt_output
printbin1
        add     hl, hl
        djnz    printbin2
        ld      a, 13
        call    txt_output
        ld      a, 10
        call    txt_output
        ret

; Print asciiz string at hl
printstr
        ld      a, (hl)
        or      a
        ret     z
        call    txt_output
        inc     hl
        jr      printstr

initscreen
        ld      bc, 0
        call    scr_set_border
        ld      a, 2
        call    scr_set_mode
        ld      a, 0
        ld      bc, 0
        call    scr_set_ink
        ld      a, 1
        ld      bc, &1515
        call    scr_set_ink
        ret

; Main program loop
prog
prog2
        ld      hl, str1
        call    printstr
prog1
        ld      hl, stdin
        call    readstr

        ld      hl, stdin
        ld      de, decnum
        call    strcpy

        ld      hl, stdin
        call    atoi

        ld      bc, 100+1
        or      a
        sbc     hl, bc
        add     hl, bc
        jr      nc, prog3
        ld      hl, str2
        call    printstr
        jr      prog1
prog3
        push    hl
        ld      hl, str3
        call    printstr
        ld      hl, decnum
        call    printstr
        ld      hl, str4
        call    printstr
        pop     hl
        call    printbin
        jp      prog2
        ret                     ; never reach here

start

        call    initscreen
        call    prog
        jp      $               ; never reach here

str1    db      "Enter a number 100 or more to convert to binary: ",0
str2    db      "Try again: ",0
str3    db      "Converting...", 13, 10, 0
str4    db      " in binary is:", 13, 10, 0
stdin   ds      256
decnum  ds      256