我正在用 NASM 编写一个汇编程序,作为其中的一部分,我希望能够读取命令行参数,在我的情况下,这可以是两件事之一,调试消息“-d”或以十六进制表示的数字,例如:
`./programName AF -d`
问题是当我使用 pop 堆栈中的第一个参数不是 argc 或 programName 时,它假设是一个表示 HEX 数字的字符串,我尝试处理它字节字节最终以段错误结束。这是我的尝试:
main:
push ebp
mov ebp, esp
mov ecx, [ebp +8] ;argc
mov eax, [ebp + 16] ; first arg that was put in by the user
dec ecx ; update num of "real" args
.nextArg:
cmp ecx, 0
je .noMoreArgs
push eax
call process_arg
add esp, 4
dec ecx
jmp .nextArg
.noMoreArgs:
...
process_arg 函数:(容量是内存中的一个变量,以字节为单位表示分配的堆栈的容量,debugStr 是一个用于比较的字符串)
push ebp
mov ebp, esp
mov edx, [ebp+8] ; edx points to the argument string
.debug:
mov bl, byte [edx] ; load first byte of debugStr (bl_1 = '-', bl_2 = 'd')
cmp bl, '-' ; cmp char by char
jnz .stack
inc edx ; get ready for next char
mov bl, byte [edx] ; load second byte of debugStr (bl_1 = '-', bl_2 = 'd')
cmp bl, 'd' ; cmp char by char
jnz .stack
mov [debug_mode], dword 1
jmp end
.stack:
mov edx, [ebp+8] ; ecx points to the argument string
xor ebx, ebx
xor eax, eax
mov ecx, 2
.loop:
mov bl, [edx] ; get byte pointed to by edx
cmp bl, '9' ; if larger than 9 => digit is a letter (assuming valid input)
ja .letter ; jmp to proceesing a letter digit
sub bl, '0'
ja .mul
.letter:
sub bl, 37h
.mul:
mov edi, 16
mul edi
add eax, ebx
inc edx
loop .loop
mov edi, 4
mul edi
; at this point eax should hold the INT value of stack_size
mov [capacity], eax
end:
pop edx
pop ecx
pop ebx
mov esp, ebp
pop ebp
ret
知道可能是什么原因吗?或有关阅读 cmdline args 的说明?以及为此目的使用 lea(加载有效地址)以及它与简单的 mov 有何不同?
提前致谢。