程序:在0到255之间输入基数为10的整数,并将数字改为基数n(其中n为2到9)。
我知道如何从基数10转换为2.但不是基数10转换为任何其他基数。 任何链接或示例都可能足以让我开始。
答案 0 :(得分:1)
为了提供原始答案的翻译,并注意给一个人喂鱼并喂他一天,以下是NASM
翻译。 注意:这是原始的32位代码:
; build and link (32-bit on x86_64)
;
; nasm -f elf -o baseconvert_32.o baseconvert_32.asm
; ld -m elf_i386 -o baseconvert_32 baseconvert_32.o
section .data
base10 db 255
newbase db 7
result times 8 db 0
newline db 10
section .text
global _start
_start:
mov edi, result ; offset in result string
xor ax, ax ; zero/clear ax register
mov al, [base10] ; at start, current remainder = whole value
mov bl, [newbase]
loop:
div bl ; divide current remainder by new base
mov [edi], ah
cmp al, 0 ; is the quotient zero?
je printchar ; if it is we are done
xor ah, ah
inc edi ; move offset in result string (note digits
jmp loop ; of answer are stored in reverse order)
printchar:
add byte [edi], 48 ; add ascii '0' to digit to get printable char
mov eax, 4 ; linux sys_write
mov ebx, 1 ; stdout
mov ecx, edi ; point to current digit
mov edx, 1 ; number of chars to print
int 0x80 ; syscall
dec edi ; point to next digit
cmp edi, result ; are we past the final digit?
jge printchar ; if not, keep printing to stdout
; print newline
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
xor ebx, ebx ; set zero exit code
mov eax, 1 ; set syscall number to __NR_exit 1 (0x1 hex)
int 0x80 ; call kernel (syscall 32-bit)
<强>输出:强>
$ ./baseconvert_32
513
答案 1 :(得分:0)
嗯,这不是NASM,它是气体,但以下代码适用于Linux。也许你或者一些友好的stackoverflower可以translate it into NASM。
使用gcc -nostdlib -g -o filename filename.s
在Linux上编译。
您可以将255和7更改为您想要的股息和基数。
如果您没有运行Linux / Unix,则系统调用不太可行。
algorithm非常简单。
.data
base10:
.byte 255
newbase:
.byte 7
result:
.byte 0,0,0,0,0,0,0,0 # since dividend <=255 and divisor >=2
# 8 digits in the new base is enough
newline:
.byte 10
.text
.global _start
_start:
movl $result, %edi # offset in result string
xorw %ax, %ax
movb base10, %al # at start, current remainder = whole value
movb newbase, %bl
loop:
divb %bl # divide current remainder by new base
movb %ah, (%edi)
cmpb $0, %al # is the quotient zero?
je printchar # if it is we are done
xorb %ah, %ah
incl %edi # move offset in result string (note
jmp loop # digits of answer are stored in reverse
# order)
printchar:
addl $48, (%edi) # add ascii '0' to digit to get printable char
movl $4, %eax # linux sys_write
movl $1, %ebx # stdout
movl %edi, %ecx # point to current digit
movl $1, %edx # one char to print
int $0x80 # syscall
decl %edi # point to next digit
cmpl $result, %edi # are we past the final digit?
jge printchar # if we aren't keep going
# print a newline
movl $4, %eax
movl $1, %ebx
movl $newline, %ecx
movl $1, %edx
int $0x80
# finish nicely
movl $0, %ebx
movl $1, %eax
int $0x80