這是我在這個平臺上的第一個問題。我正在嘗試修改影像檔案的像素并將它們復制到使用 calloc 請求的記憶體中。當代碼嘗試取消參考指向在偏移量 16360 處使用 calloc 請求寫入的記憶體的指標時,會引發“訪問沖突寫入位置”例外。有時偏移量會略高或略低。請求的記憶體量是正確的。當我用 calloc 在 C 中撰寫等效代碼時,它可以作業,但不能在匯編中使用。我還嘗試在匯編中請求更多的記憶體,并在 Visual Studio 設定中提高堆和堆疊大小,但對于匯編代碼沒有任何作用。在構建和運行程式之前,我還必須設定選項 /LARGEADDRESSAWARE:NO。
我知道 AVX 指令集更適合這個,但是代碼會包含更多的行,所以我讓這個問題更簡單,我也不是專業人士,我這樣做是為了練習 AVX 指令集。
提前謝謝了 :)
const uint8_t* getImagePtr(sf::Image** image, const char* imageFilename, uint64_t* imgSize) {
sf::Image* img = new sf::Image;
img->loadFromFile(imageFilename);
sf::Vector2u sz = img->getSize();
*imgSize = uint64_t((sz.x * sz.y) * 4u);
*image = img;
return img->getPixelsPtr();
}
EXTRN getImagePtr:PROC
EXTRN calloc:PROC
.data
imagePixelPtr QWORD 0 ; contains address to source array of 8 bit pixels
imageSize QWORD 0 ; contains size in bytes of the image file
image QWORD 0 ; contains pointer to image object
newImageMemory QWORD 0 ; contains address to destination array
imageFilename BYTE "IMAGE.png", 0 ; name of the file
.code
mainasm PROC
sub rsp, 40
mov rcx, OFFSET image
mov rdx, OFFSET imageFilename
mov r8, OFFSET imageSize
call getImagePtr
mov imagePixelPtr, rax
mov rcx, 1
mov rdx, imageSize
call calloc
add rsp, 40
cmp rax, 0
je done
mov newImageMemory, rax
mov rcx, imageSize
xor eax, eax
mov bl, 20
SomeLoop:
mov dl, BYTE PTR [imagePixelPtr rax]
add dl, bl
mov BYTE PTR [newImageMemory rax], dl ; exception when dereferencing and writing to offset 16360
inc rax
loop SomeLoop
done:
ret
mainasm ENDP
END
uj5u.com熱心網友回復:
讓我們將此行翻譯回 C:
mov BYTE PTR [newImageMemory rax], dl ;
在 C 中,這或多或少等同于:
*((unsigned char *)&newImageMemory rax) = dl;
這顯然不是你想要的。它正在寫入從 的位置開始的偏移量newImageMemory,而不是從newImageMemory指向的位置開始的偏移量。
如果要將 newImageMemory 用作偏移量的基地址,則需要將其保存在暫存器中。
雖然我們這樣做了,但出于同樣的原因,這條線也是錯誤的:
mov dl, BYTE PTR [imagePixelPtr rax]
它只是碰巧沒有崩潰。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434839.html
上一篇:` `和` =`運算子的行為不同
