- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
一个C循环是
while( *from)
{
*to++ = *from++;
}
我想我基本上想知道 *to++ = *from++;
转换成哪些 MIPS 指令。我的结果是指令是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
我通过查看完整的 C 程序得出了这个结论:
/* 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);
}
将其编译为 MIPS 汇编结果如下:
.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
于是我分析了上面的内容,发现while循环一个周期执行的指令数是14条,我的推理是否正确?
最佳答案
$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
因此循环中有 13 个操作,因为跳过了 j $L3。
然而,正如 markgz 指出的那样,MIPS 有分支延迟槽,这可能需要编译器或汇编器添加 nop 或 switch 指令。您应该查看最终代码的反汇编以及中间汇编器输出。
在这种情况下,很可能在初始 bne 指令之后至少会有一个额外的 nop,但汇编器可能会重新排序最后的跳转,而不是用 nop 填充。因此,如果您查看最终输出,总共可能有 14 条指令。
那里有很多冗余 - 一半的指令只是加载/存储回局部变量,而不是仅仅将内容保存在寄存器中。这是典型的调试/未优化构建。
关于c - 这个循环在一个周期内执行了多少条指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13821211/
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我正在尝试将 JSON 发送到我的服务器并作为结果检索 JSON。例如发送用户名和密码并取回 token 和其他内容。 这就是我正在为发送的 HTTP 请求所做的。我现在如何检索同一请求中的内容?
我有以下 xts 矩阵: > options(digits.secs = 6) > set.seed(1234) > xts(1:10, as.POSIXlt(1366039619, tz="EST"
我目前正在开发一个应用程序,当用户到达某个位置时,它会提醒用户。我希望这个应用程序也在后台运行并搜索解决方案。 在 AppStore 中,我发现了一款名为“Sleep Cycle”的应用程序,它可
我想创建一个基于 farbtastic color picker 的颜色选择器。我想要实现的是添加我想要链接到色轮的 RGB slider 。这是我到目前为止所拥有的。 app.controller(
RFC 5545 允许 RDATE 属性具有 PERIOD 数据类型。该数据类型的语义是什么?据我所知,这是未指定的。它会改变事件的持续时间吗?如果时区更改且没有持续时间怎么办? 最佳答案 尽管我
在 CodinGame学习平台,C# 教程中用作示例的问题之一是: The aim of this exercise is to check the presence of a number in a
我听说网上有一本英特尔书,它描述了特定汇编指令所需的 CPU 周期,但我找不到(经过努力)。谁能告诉我如何找到CPU周期? 这是一个例子,在下面的代码中,mov/lock 是 1 个 CPU 周期,x
据我所知,Java GC有次要GC(低成本)和主要GC周期(高成本)。如果对象在本地范围内,则会在 Minor GC 中清理它。如果对象的引用存储在代码中的其他位置,则它会在主 GC 中被清除。 例如
到目前为止,我有一个很好的自旋锁,可以用作 intendend: std::atomic_flag barrier = ATOMIC_FLAG_INIT; inline void lo
晚上好,我将 cycle2 与 prev 和 next 函数一起使用,但我无法将 prev 和 next 函数置于图像下方的中心。我环顾四周,我知道这会很愚蠢,但我就是看不到它。非常令人沮丧。谢谢加里
出于教育目的,我想知道在优化(在不同级别)和编译之后执行函数需要多少 CPU 周期。有没有办法分析代码或可执行文件以获得可重现的答案?我在 64 位 Windows 7 Pro 上使用 Eclipse
我想彻底测量和调整我的 C/C++ 代码,以便在 x86_64 系统上更好地使用缓存。我知道如何使用计数器(我的 Windows 机器上的 QueryPerformanceCounter)来测量时间,
我尝试将一些数据分组到每四周一次的存储桶中,并使用 pd.Grouper(key='created_at', freq='4W')。我希望这些组是这样的,如果我有从 2019-08-26 到 2019
我正在做一个关于随机数的大型学校项目,但我找不到 Math.random() 的句点。我安装了 7.0.800.15 版本,并且正在使用 Windows 10 计算机。我试过用一个简单的程序来确定周期
我正在努力解决我们生产环境中垃圾收集利用率高的问题,我想知道设置一个大的堆大小来保证老年代永远不会被填满是否会阻止触发主要的 GC 周期。 为了实现这一点,我想有一个特定的阈值标记会触发主要的 GC
我想测量在 Python 3 中执行加法运算所需的时钟周期数。 我写了一个程序来计算加法运算的平均值: from timeit import timeit def test(n): for i
我正在寻找一种方法来测量线程上的函数调用所花费的 cpu 周期。 示例伪代码: void HostFunction() { var startTick = CurrentThread.Cur
就 CPU 周期而言,malloc() 的成本是多少?(Vista/OS,最新版本的 gcc,最高优化级别,...) 基本上,我正在实现一个复杂的 DAG 结构(类似于链表)由一些 16B(不太常见)
C/C++ 中的类型转换会导致额外的 CPU 周期吗? 我的理解是,至少在某些情况下应该消耗额外的 CPU 周期。就像从浮点类型转换为整数一样,CPU 需要将浮点结构转换为整数。 float a=2.
我是一名优秀的程序员,十分优秀!