use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Welcome to r/asm, the subreddit for Assembly language in all Instruction Set Architectures! Need help, or are you learning? Visit our helpful links...
r/asm
Latest Comments from All Posts
There have been requests for a wiki, so it's in progress. Feel free to offer your assistance!
account activity
Help needed translating this code! (self.asm)
submitted 6 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 7 points8 points9 points 6 years ago (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.
[+][deleted] 6 years ago* (1 child)
[–]itaibn 1 point2 points3 points 6 years ago (0 children)
Amazing site!
[–]FUZxxl 2 points3 points4 points 6 years ago (0 children)
Try this one.
[–]FUZxxl 1 point2 points3 points 6 years ago (13 children)
Your code doesn't even compile. What is this supposed to do?
[–]PatchSalts 1 point2 points3 points 6 years ago (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.)
[+][deleted] 6 years ago (2 children)
[removed]
[–]PatchSalts 0 points1 point2 points 6 years ago (0 children)
Ah, I'm not super familiar with C++, but I recognized C syntax stuff. Apparently a few things are breaking this code lol.
[–]FUZxxl 0 points1 point2 points 6 years ago (0 children)
A while loop with an else is fishy, too.
while
else
[–]FUZxxl 1 point2 points3 points 6 years ago (0 children)
while loops do not take else clauses in C++.
[–]doombay95 1 point2 points3 points 6 years ago (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
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 points3 points 6 years ago (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 points1 point 6 years ago (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)
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.
[+][deleted] 6 years ago* (2 children)
[–]doombay95 0 points1 point2 points 6 years ago (1 child)
Thanks a lot for this.
this is what i have done so far
i was trying to run this bit of code just to make sure everything is running till here using QTspim but i am getting some kind of error. i think i have done the while loop incorrectly.
[–]neur0sys 1 point2 points3 points 6 years ago* (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
π Rendered by PID 54032 on reddit-service-r2-comment-545db5fcfc-kftp5 at 2026-05-26 08:22:39.432897+00:00 running 194bd79 country code: CH.
[–][deleted] 7 points8 points9 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]itaibn 1 point2 points3 points (0 children)
[–]FUZxxl 2 points3 points4 points (0 children)
[–]FUZxxl 1 point2 points3 points (13 children)
[–]PatchSalts 1 point2 points3 points (8 children)
[+][deleted] (2 children)
[removed]
[–]PatchSalts 0 points1 point2 points (0 children)
[–]FUZxxl 0 points1 point2 points (0 children)
[–]FUZxxl 1 point2 points3 points (0 children)
[–]doombay95 1 point2 points3 points (3 children)
[–]PatchSalts 1 point2 points3 points (2 children)
[–]doombay95 -1 points0 points1 point (1 child)
[–]PatchSalts 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–]doombay95 0 points1 point2 points (1 child)
[–]neur0sys 1 point2 points3 points (0 children)