【发布时间】:2020-09-09 22:06:58
【问题描述】:
作为我的项目的一部分,这是一个无限精度的 RPN 计算器, 我正在尝试编写一种方法来接收大小最多为 80 字节的缓冲区。由于我想支持无限精度(或至少受堆大小的限制),我想获取该缓冲区并创建一个指向表示数字的链表头部的指针,例如,如果里面的输入缓冲区是:0x7D12AF
然后由该输入生成的链表将如下所示:
[AF, addr1] -addr1->[12, addr2] -addr2-> [7D, addr3] -addr3-> 0
其中每个链接是 5 个字节,4 个用于指向下一个链接的指针,一个字节用于数据。 这是我的想法,我会接受任何建议,因为我真的不确定我在做什么: (假设 atoi 接受一个十六进制数字并将其转换为数值)
`section .bss
ptr: resb 5
section .data
setcion .text
align 16
extern malloc
_buffersize
push ebp
mov ebp, esp
mov ecx, [ebp+8]
_buffersize:
push ebp
mov ebp, esp
push ecx
mov ecx, [ebp+8]
xor eax, eax
.loop:
cmp [ecx], 20h
jle .done
inc ecx
inc eax
jmp .loop
.done:
pop ecx
mov esp, ebp
pop ebp
ret
_listify:
push ebp
mov ebp, esp
mov edx, [ebp+8] ; pointer to the first byte in the number_string
pushad
push edx ; push function argument
call _buffersize ; eax now holds the size of the buffer
add esp, 4 ; clean up stack after call
mov ecx, eax ; count for the loop
.loop:
pushad ; allocate 5 bytes for a node : 4 for a next ptr, 1 for data
push 5
call malloc ; eax now points to the 5 bytes allocated
add esp, 4 ; clean up stack after call to malloc
mov [ptr], eax ; now ptr points to the address in memory of the 5 allocated bytes
popad
push [edx] ; push the first byte pointed to by edx as an argument for atoi (atoi converts a signle HEX digit to it's numeric value)
call _atoi
add esp, 4 ; eax now holds the numeric value of that 1 byte character
mov ebx, [ptr] ; ebx points to the allocated memory
mov [ebx], dword 0 ; the address of the next link is NULL as we're insterting at the head of the lsit
mov [ebx], byte eax ; hopefully, ebx should now points to 5 bytes in memory of the form [b4b3b2b1b0] where b4b3b2b1 is the address of the next link & b0 is a 0 <= number <16
mov [ptr], ebx ; now ptr points to the address of the newly updated linked list representing the number
inc edx ; get ready to read next byte
loop .loop
popad
mov esp, ebp
pop ebp
ret
`
我还有一个问题是:有没有办法以十六进制表示形式存储数字?我认为这是一个愚蠢的问题,因为表示只是我看待它的方式,但值是相同的..所以将十六进制数字 ASCII 表示转换为 int 是一样的,并且要制作十六进制我应该对待它从 char 转换为 int 时的这种方式,反之亦然。如果我错了,请纠正我。 谢谢!
【问题讨论】:
标签: assembly x86 nasm bigint rpn