The MASM Forum

General => The Campus => Topic started by: cody1 on April 04, 2024, 11:39:51 PM

Title: Converting C-Code into Assembler
Post by: cody1 on April 04, 2024, 11:39:51 PM
Consider this C-Code

char str[] = "this is a string!";
int titlecase(char* c) {
  int words = 0;
  int last_was_whitespace = 1;
  while (*c) {
    if (last_was_whitespace && *c >= 'a' && *c <= 'z') {
      *c ^= 32;
      last_was_whitespace = 0;
    } else {
      if (*c == ' ') {
        words += 1 - last_was_whitespace;
        last_was_whitespace = 1;
      } else {
        last_was_whitespace = 0;
      }
    }
    c++;
  }
  return words + 1 - last_was_whitespace;
}
int main() {
  int words = titlecase(str);
  puts(str);
}

I need to translate this C code into RISC-V Assembler. This is my attempt

.data
    str: .string "this  is a  string!"

.globl main

.text

titlecase:
    addi sp,sp,-16
    sw ra,12(sp) # return adress
    sw a0,8(sp) # *c
    addi s0,s0,0 # int words
    addi s1,s1,1 # int last_was_whitespace
    lw s2,'a'
    lw s3,'z'
    lw s4,' '
    addi s5,zero,1 # s5 = 1

while:
    lw t0,0(a0) # *c
    and t1,s1,t0 # last_was_whitespace && *c
    bne t0,zero, end_while # while condition
        blt t1, s2, L1 # (last_was_whitespace && *c >= 'a')
        L1: bge t0,s3,L2 #  (*c <= 'z')
        # if(last_was_whitespace && *c >= 'a' && *c <= 'z') was not met; else
        bne t0,t4,L4
            sub t3,s5,s1 ## 1-last_was_whitespace
            add s0,s0,t3 ## words = words + last_was_whitespace
            addi s1,zero,1 ## last_was_whitespace = 1
            addi t0,t0,1 #c++
        L4:
            addi s1,zero,0 ## last_was_whitespace = 0
       
        L2:
            xori t2,t0,32 # *c^= 32
            addi s1,zero,0 # last_was_whitespace = 0                   
end_while:
    lw ra,12(sp) # return adress
    lw a0,8(sp) # *c
    addi sp,sp, 16
    add a0,s0,t3
 
    jalr zero,ra,0 #exit
   
main:
    la s0,str
      jal ra,titlecase
    add s6,zero,a0
   
    addi a0, zero, 1
    add  a1, zero, s6
      ecall

I am getting this error "label a used but not defined". I've tried debugging for a while now and I just dont see where I am using this label a. Any insights ?

Thanks in advance

Title: Re: Converting C-Code into Assembler
Post by: cody1 on April 05, 2024, 12:32:07 AM
Actually I found the error, it was at the top; the lw was causing the issues im susposed to use li (but since this is homework and pseudo instructions are not allowed) I solved it with addi s2,zero 'a' (the same pattern for the other two)

Now I get the code to compile but I am 99% sure it is not working correctly.It  should be giving  me out the number of words in the string, not including whitespaces; hence in my example 4. But the value of my register s6 that should contain the value of words in the string is 268435456. I am still new to assembler, registers,memory and all of that but I am pretty sure that in this s0 register, the value should be 4. Could anyone confirm my suspicion so that I can get to debugging again?

Thanks in advance!
Title: Re: Converting C-Code into Assembler
Post by: zedd151 on April 05, 2024, 12:47:52 AM
Not really sure if you will find an answer here. This forum is for Masm compatible assembly.

Try googling for "RISC-V assembler forum" for better help.

There may be however someone here that has experience with RISC assembler that may give you some guidance, but not highly likely.

That being said, it is fairly easy to convert C code to Masm assembly. But the instruction set is quite different to RISC-V. I did a quick search for RISC-V instructions.