输出整数装配在Linux上 [英] Outputting integers in assembly on Linux

查看:142
本文介绍了输出整数装配在Linux上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这需要在纯组装完成(即没有图书馆或调用C)。

This needs to be done in pure assembly (ie. no libraries or calls to C).

我理解问题的本质是:人们需​​要由10来划分整数,​​转换一个数字其余为ASCII,输出再与商数重复该过程

I understand the essence of the problem: one needs to divide the integer by 10, convert the one-digit remainder to ASCII, output that and then repeat the process with the quotient.

但由于某些原因,它只是不工作。我使用的x86 NASM。

But for some reason, it's just not working. I'm using NASM on x86.

下面就是我到现在(不输出任何东西,但不抛出任何错误汇编其一):

Here's what I have up to now (doesn't output anything, but doesn't throw any assembler errors either):

; integer to output is stored in eax
mov ecx, 10   ; for base 10

loop:
div ecx  ;EAX contains the quotient, EDX the remainder

; Do something to EDX to convert it to ASCII, not sure if this is correct
add edx, '0'

push eax    ;We'll be playing with EAX to output EDX, save EAX to the stack

mov eax, 4              ; sys_write
mov ebx, 1              ; to STDOUT
mov ecx, edx
mov edx, 1
int 0x80

pop eax  ;restore EAX

cmp eax, 0   ;If EAX is 0, our job is done
jnz loop

有若干与此类似(即,<一问题href=\"http://stackoverflow.com/questions/4117422/more-efficient-way-to-output-an-integer-in-pure-assembly\">this和),但我在执行丢失。 (适用于DOS)这个问题也是有帮助的,但我仍然感到困惑。

There are a number of questions similar to this one (namely, this and this), but I'm lost in the implementation. This question (for DOS) was also helpful, but I'm still confused.

我必须在这里失去了一些东西。思考?

I must be missing something here. Thoughts?

推荐答案

有至少两个更多的问题。超越 ECX 的腐败是@sarnold提到:

There are at least two more problems. beyond the corruption of ecx that @sarnold mentioned:


  1. DIV ECX 除以64位值 EDX:EAX ECX ,所以你需要确保你的事业部之前设置 EDX 0。

  1. div ecx divides the 64-bit value edx:eax by ecx, so you need to ensure that you set edx to 0 before the division.

的第二个参数的系统调用(在 ECX )应该是一个指针包含要打印的字符,而不是字符本身缓冲区。

The second argument to the write system call (in ecx) should be a pointer to a buffer containing the character you want to print, not the character itself.

要解决第二个问题的方法之一是推动寄存器包含要打印在栈上,然后分配堆栈指针尤其的字符 ECX (在最近推的项目堆栈指针指向,和x86卖场值小尾数,因此第一个字节是低8位)。例如。

One way to solve the second problem is to push the register containing the character you want to print on the stack, and then assign the stack pointer esp to ecx (the stack pointer points at the most recently pushed item, and x86 stores values little-endian, so the first byte is the low 8 bits). e.g.

push edx         ; save value on stack
mov  eax, 4      ; sys_write
mov  ebx, 1      ; to STDOUT
mov  ecx, esp    ; first byte on stack
mov  edx, 1      ; length = one byte
int  0x80
pop  edx         ; remove what we pushed (or "add esp, 4" would do just as well here;
                 ;                        we don't need the actual value again)

这应该足以让一些输出...

That should be enough to get some output...

(但在这一点上,你可能会发现你的算法的功能,并要重新思考如何存储由分割产生的数字!)

(But at that point, you might notice a "feature" of your algorithm, and want to re-think how you store the digits that are produced by the division!)

这篇关于输出整数装配在Linux上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆