[英]flip an image assembly code
我正在開發基於 ac 的程序,該程序可用於圖像扭曲的組裝。 應該工作的偽代碼是這個(總是使用 240x320 的圖像
voltearHorizontal(imgO, imgD){
dirOrig = imgO;
dirDest = imgD;
dirOrig = dirOrig + 239*320; //bring the pointer to the first pixel of the last row
for(f=0; f<240; f++){
for(c=0; c<320; c++){
[dirDest]=[dirOrig];
dirOrig++;
dirDest++;
}
dirOrig=dirOrig+640;//move the pixel to the first one of the upper row
}
}
但是當應用於匯編時,結果中的第一行不被讀取,留下黑色空間。
https://gyazo.com/7a76f147da96ae2bc27e109593ed6df8
這是我編寫的代碼,它應該可以工作,而這就是圖像真正發生的情況:
https://gyazo.com/2e389248d9959a786e736eecd3bf1531
為什么不使用此代碼將原始圖像的上一行像素寫入/讀取到第二個圖像? 我弄錯了哪部分代碼?
我想我沒有標簽可以解決我的問題,感謝您提供的任何幫助(在我錯的地方)。此外,水平翻轉(上面是垂直)只是意外地完成了程序:
有什么特殊原因,為什么你把它寫成慢匯編程序?
你為什么不把它保存在快速的 C++ 中? https://godbolt.org/g/2oIpzt
#include <cstring>
void voltearHorizontal(const unsigned char* imgO, unsigned char* imgD) {
imgO += 239*320; //bring the pointer to the first pixel of the last row
for(unsigned f=0; f<240; ++f) {
memcpy(imgD, imgO, 320);
imgD += 320;
imgO -= 320;
}
}
將使用 gcc6.3 -O3 編譯為:
voltearHorizontal(unsigned char const*, unsigned char*):
lea rax, [rdi+76480]
lea r8, [rdi-320]
mov rdx, rsi
.L2:
mov rcx, QWORD PTR [rax]
lea rdi, [rdx+8]
mov rsi, rax
sub rax, 320
and rdi, -8
mov QWORD PTR [rdx], rcx
mov rcx, QWORD PTR [rax+632]
mov QWORD PTR [rdx+312], rcx
mov rcx, rdx
add rdx, 320
sub rcx, rdi
sub rsi, rcx
add ecx, 320
shr ecx, 3
cmp rax, r8
rep movsq
jne .L2
rep ret
IE。 比如比你的內聯匯編效率高 800%。
無論如何,在您的問題中,問題是:
dirOrig=dirOrig+640;//move the pixel to the first one of the upper row
您需要執行-= 640
才能返回兩行。
關於屏幕中的那些內聯匯編……將它們作為文本進行質疑,但從快速瀏覽它們的角度來看,我只想用 C++ 重寫它並將其保留給編譯器,您在匯編中做了許多性能錯誤的事情,所以我沒有看到這樣做的任何意義,加上內聯 asm 丑陋且難以維護,並且難以正確編寫。
我什至檢查了圖中的那個 asm。 您在eax
有行計數器,但您使用al
復制像素,因此它確實會破壞行計數器值。
下次使用調試器。
順便說一句,您的圖片是 320x240,而不是 240x320。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.