简体   繁体   中英

MIPS Assembly Number of Instructions

.text  
.global main  
main:      
        addi    $v0, $zero,0  
        la  $t0, length  
        lw  $t1, 0($t0)  
        la  $t4, array  
        addi $t1, $t1, -1  
        sll $t1,$t1,2  

loop:   
        add $t3, $t4,$t1  
        lw  $t2,0($t3)  
        add $v0,$v0,$t2  
        addi $t1,$t1,-4  
test:   
        slti $t9,$t1,0  
        beq $t9,$zero,loop  
        nop   
        sw  $v0, 0($t0)  
.data  
length: .word 8  
array:  .word 9,8,7,6,5,4,3,2  

I have to tell how many static instructions are in this code. I know that before data there are 16 instructions, I am wondering if the directives written after .data are also considered as 2 instructions, making a total of 18 instructions.

Yesno.

The directive " .data " itself doesn't compile into anything machine related, it only marks another section of source, which with some toolchains will lead into linking that part of compiled source in different linking stage, possible landing into specific executable part (noted by headers of executable, depends on target platform executables format).

And those .word 9,8,7,6,5,4,3,2 do compile as word-sized numerical values 9, 8, ...

But from the CPU point of view numbers in memory are as good as instructions, so would you try to execute it, the CPU would happily hop on it, and execute those numbers as instructions (of garbage meaning, probably crashing machine). IIRC MIPS is risc-like with fixed instruction size to single word, so the line .word 9,8,7,6,5,4,3,2 contains 8 instructions.

But you have to tell, how many instructions were executed . And that does depend on the flow of execution of the code. In your case the code looks quite simple and straightforward till the end, with simple loop (should you count the instructions several times or just once, per loop?).

Where it ends with sw $v0, 0($t0) instruction. That's store word? So CPU will follow on next instruction (next word value in memory). What is there is not defined in the source, if the .data segment starts there, then words 8, 9, 8, ... will be executed as instructions (too lazy to disassemble that, what it would turn into).

So the reasonably human-readable source has 16 instructions, which I would be afraid to execute on the HW, as the code looks unfinished.

And... where did you count 16 instructions? I see 14 only?


Would be probably easier to check disassembly, as Michael pointed out some loads may assemble into several opcodes, depending on the immediate value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM