Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:32:45+00:00 2026-06-16T00:32:45+00:00

A C loop is while( *from) { *to++ = *from++; } I think I

  • 0

A C loop is

  while( *from)
  {
    *to++ = *from++;
  }

I think I basically want to know which MIPS instructions the *to++ = *from++; translates to. My result is that the instructions are 14:

$L2:
   lw    $2,12($fp)
   lb    $3,0($2)
   bne    $3,$0,$L4
   j    $L3
$L4:
   lw    $2,8($fp)
   addu    $3,$fp,12
   lw    $4,0($3)
   lbu    $5,0($4)
   sb    $5,0($2)
   addu    $4,$4,1
   sw    $4,0($3)
   addu    $2,$2,1
   sw    $2,8($fp)
   j    $L2

I came to this conclusion from looking at the complete C program:

/* strcpy.c */

#include <stdio.h>
#include <idt_entrypt.h>

/* C stringcopy */

static void str_cpy( char *to, const char *from)
{
  while( *from)
  {
    *to++ = *from++;
  }
  *to = '\0';
}

int main()
{
  static char* hello = "Hello World!";
  static char to[4711] = "blaha blaj blurk bletch";
  int Time;

  printf("Strangen hello ser ut sa har: %s\n", hello);
  flush_cache();          /* toem cache-minnet */
  timer_start();          /* nollstall tidmatning */

  str_cpy( to, hello);

  Time = timer_stop();            /* las av tiden */
  printf("Time to copy: %d\n",Time);
  printf("Och kopian sa har: %s\n", to);
}

Compiling it to MIPS assembly turns out this:

    .file   1 "strcpy.c"

 # -G value = 8, Cpu = 3000, ISA = 1
 # GNU C version cygnus-2.7.2-970404 (mips-mips-ecoff) compiled by GNU C version cygnus-2.7.2-970404.
 # options passed:  -msoft-float
 # options enabled:  -fpeephole -ffunction-cse -fkeep-static-consts
 # -fpcc-struct-return -fcommon -fverbose-asm -fgnu-linker -msoft-float
 # -meb -mcpu=3000

gcc2_compiled.:
__gnu_compiled_c:
    .text
    .align  2
    .ent    str_cpy
str_cpy:
    .frame  $fp,8,$31       # vars= 0, regs= 1/0, args= 0, extra= 0
    .mask   0x40000000,-8
    .fmask  0x00000000,0
    subu    $sp,$sp,8
    sw  $fp,0($sp)
    move    $fp,$sp
    sw  $4,8($fp)
    sw  $5,12($fp)
$L2:
    lw  $2,12($fp)
    lb  $3,0($2)
    bne $3,$0,$L4
    j   $L3
$L4:
    lw  $2,8($fp)
    addu    $3,$fp,12
    lw  $4,0($3)
    lbu $5,0($4)
    sb  $5,0($2)
    addu    $4,$4,1
    sw  $4,0($3)
    addu    $2,$2,1
    sw  $2,8($fp)
    j   $L2
$L3:
    lw  $2,8($fp)
    sb  $0,0($2)
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $fp,0($sp)
    addu    $sp,$sp,8
    j   $31
    .end    str_cpy
    .rdata
    .align  2
$LC0:
    .ascii  "Hello World!\000"
    .sdata
    .align  2
hello.4:
    .word   $LC0
    .data
    .align  2
to.5:
    .ascii  "blaha blaj blurk bletch\000"
    .space  4687
    .rdata
    .align  2
$LC1:
    .ascii  "Strangen hello ser ut sa har: %s\n\000"
    .align  2
$LC2:
    .ascii  "Time to copy: %d\n\000"
    .align  2
$LC3:
    .ascii  "Och kopian sa har: %s\n\000"
    .text
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $fp,32,$31      # vars= 8, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,32
    sw  $31,28($sp)
    sw  $fp,24($sp)
    move    $fp,$sp
    jal __main
    la  $4,$LC1
    lw  $5,hello.4
    jal printf
    jal flush_cache
    jal timer_start
    la  $4,to.5
    lw  $5,hello.4
    jal str_cpy
    jal timer_stop
    sw  $2,16($fp)
    la  $4,$LC2
    lw  $5,16($fp)
    jal printf
    la  $4,$LC3
    la  $5,to.5
    jal printf
$L5:
    move    $sp,$fp         # sp not trusted here
    lw  $31,28($sp)
    lw  $fp,24($sp)
    addu    $sp,$sp,32
    j   $31
    .end    main

So I analyzed the above and found that the number of instructions performed in one cycle of the while loop is 14. Is my reasoning correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:32:46+00:00Added an answer on June 16, 2026 at 12:32 am
    $L2:
     lw    $2,12($fp)  ;  12($fp) is 'from' - load it in to $2
     lb    $3,0($2)    ; read a byte
     bne    $3,$0,$L4  ; if it's non-zero, jump into the main loop
     j    $L3          ; otherwise exit (this is the while clause)
    $L4:
     lw    $2,8($fp)   ; 8($fp) is 'to' - load it into $2
     addu    $3,$fp,12 ; Load the address of 'from' into $3
     lw    $4,0($3)    ; Load 'from' into $4
     lbu    $5,0($4)   ; Read the byte again (this is the = *from)
     sb    $5,0($2)    ; Store the byte (*to = )
     addu    $4,$4,1   ; increment from (from++)
     sw    $4,0($3)    ; store it back
     addu    $2,$2,1   ; increment to (to++)
     sw    $2,8($fp)   ; store it back
     j    $L2          ; do it all again
    

    So 13 ops in the loop, as the j $L3 is skipped.

    However, as markgz points out, MIPS has branch delay slots, which may require the compiler or assembler to add nops or switch instructions. You should look at the disassembly of the final code, as well as the intermediate assembler output.

    It’s probable in this case that there will at least be an extra nop after the initial bne instruction, but the assembler may re-order the final jump rather than pad with a nop. So 14 instructions may well be the total if you look at the final output.

    There’s a lot of redundancy in there – half the instructions are just loading/storing back to local variables, rather than just keeping stuff in registers. This is typical of a debug/unoptimised build.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experimenting with a lexer, and I found that switching from a while-loop to
The while loop I have while reading in from a file doesn't break. I'm
I have an ArrayAdapter being populated with ints from a while loop, we will
I am using a PHP while loop in order to get data from a
I would like to call a Python script from within a Bash while loop.
I have one query. Through while loop I am displaying three random songs from
I have an nsthread, a while loop within this one. It's getting objects from
I currently have a program that basically reads html from a webpage. After sending
I've done something like this in pvp using while loop but I don't think
Say you have a simple loop while read line do printf ${line#*//}\n done <

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.