gpt4 book ai didi

windows - 试图找出 MASM 语法

转载 作者:可可西里 更新时间:2023-11-01 09:28:51 26 4
gpt4 key购买 nike

我已经在 Linux 上完成了一些汇编编程,现在正尝试使用 MASM 在 Windows 上完成。不过,我遇到了几个问题。

(我在这里尝试实现 strlen() 函数。我知道函数逻辑/指令不是最优的,但我只是想弄脏一些东西,这样我就可以继续实现其他 C 库函数.)

.386
.model flat, stdcall
option casemap:none

include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib

include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

.data
testString db "test string", 0 ; 0 -> terminator

.code
my_strlen proc
mov ebp, esp ; function prologue
push esp
push ebx
push ecx

xor ecx, ecx ; set count to 0
mov bl, byte ptr [ebp + 8] ; set low of b to 1st character

repeat:
cmp bl, 0 ; if null terminator, return
jz done
inc ecx ; increase count
mov bl, byte ptr [ebp + 8 + ecx] ; increase *ebx
jmp repeat ; repeat

done:

mov eax, ecx ; return count

pop ecx ; function epilogue
pop ebx
pop esp
ret

my_strlen endp


main:
push offset testString ; result = my_strlen(testString)
call my_strlen

push eax ; StdOut(result)
call StdOut

push 0 ; ExitProcess(0)
call ExitProcess
end main

当我尝试编译时,它似乎不喜欢我的 jmp 标签,抛出不匹配的宏嵌套等。执行此操作的正确方法是什么?附:我尽量避免使用 MASM 宏,而更喜欢自己编写指令。

有人可以编译这个程序吗?一旦我看到它是如何正确完成的,我就会走上快乐的道路,我将能够做到。是的,我在寻找资源,但在这个问题出现时我仍然在寻找资源。

最佳答案

有人可以编译这个程序吗?

Voilà(评论中的解释):

.386
.model flat, stdcall
option casemap:none

include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib

include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

.data
testString db "test string", 0

.code
my_strlen proc

; mov ebp, esp ; function prologue
; push esp

push ebp ; this is a correct prologue
mov ebp, esp

push ebx
push ecx
push esi

mov esi, [ebp+8] ; in [EBP+8] is a pointer

xor ecx, ecx
mov bl, byte ptr [esi]

repea: ; "repeat" is a keyword
cmp bl, 0
jz done
inc ecx
mov bl, byte ptr [esi + ecx] ; increase *esi
jmp repea ; repeat

done:
mov eax, ecx

pop esi
pop ecx
pop ebx

leave ; this is a correct epilogue
ret

my_strlen endp


main proc ; this is better
push offset testString
call my_strlen

; push eax ; StdOut(result)
push offset testString ; The MASM32-function StdOut needs only an ASCIZ-string
call StdOut

push 0
call ExitProcess
main endp

end main

我怀疑你想输出my_strlen的结果。这对于 StdOut 是不可能的,因为 StdOut 需要一个指向 string 的指针。您必须创建一个函数来将 EAX 转换为字符串。

关于windows - 试图找出 MASM 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25796257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com