NASM程序汇编语言 [英] NASM program Assembly Language
本文介绍了NASM程序汇编语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
程序:在基座10从0到255输入的整数,将改变数目成基n(其中n为2至9)。
Program : enter an integer in base 10 from 0 to 255 and will change the number into base n (where n is from 2 to 9).
我知道如何从基数10转换为2而不是基地10到任何其他基地。
任何链接或例子可能是足以让我开始。
I know how to convert from base 10 to 2. but not base 10 to any other base. Any links or example might be sufficient to get me started.
推荐答案
为了提供翻译到原来的答案,并铭记关于给一个人一条鱼,喂他一天,以下是 NASM
翻译。 注意:这是32位code作为原:
In order to provide the translation to the original answer, and mindful about giving a man a fish and feeding him for a day, the following is the NASM
translation. Note: this is 32-bit code as in original:
; 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
这篇关于NASM程序汇编语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文