2016-10-24 30 views

回答

1

從一個寄存器開始,該寄存器指向字符串的第一個字符,另一個寄存器設置爲零。

循環直到第一個寄存器的內容是NUL字符(或任何你想要的終止符),遞增這兩個寄存器。

最後,第二個寄存器將具有長度。

在僞彙編代碼(因爲它很可能這是課堂作業):

push r1, r3     ; preserve registers 
    load r1, address-of-string 
    xor r2, r2     ; xor a number with itself gives 0 
startloop: 
    load r3, [r1]    ; get memory contents 
    beq endloop    ; if NUL, we're at string end 
    inc r1      ; otherwise incr both and loop back 
    inc r2 
    bra startloop 
endloop: 
    pop r3, r1    ; restore registers 
           ; r2 now has the length. 

你的任務是到現在將其轉換成真正的彙編代碼(最有可能的x86給你的int 21h提)。

+0

是的,這是我的作業:D我試圖找出另一種方式,但我的代碼太長。再次請您 –

+0

順便說一下,'xor same,same'只是x86上的一個很好的歸零方式。在包括ARM在內的許多其他體系結構上[它在體系結構上需要對輸入寄存器進行依賴(以保留memory_order_consume語義)](http://stackoverflow.com/questions/37222999/convert-c-to-assembly-with- predicate-instruction/37224546#comment61983585_37224546),所以'mov r2,0'的大小相同(在ARM Thumb2上更小),並且性能更好。但顯然你只是想僞裝成x86 x86,所以+1 :) –

相關問題