A(应该是)简单的程序与NASM doesnt't工作 [英] A (should be) simple program with nasm doesnt't work
问题描述
我是新来这个论坛。
我有高级语言(还真有点)一点经验。近一个月前我认为这将是一个好主意,看看如何装配在Linux上选择NASM(IA-32),我开始从一个教程的学习后,曾如此。
现在,结束后,我试着写一个简单的程序,你在哪里得到的电脑打印的100号(1 2 4 8 16 ......)列表,但我甚至不能得到它的权利。
我得到这样的输出:
1PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP(继续)...
I'm new to this forum. I have a little experience with high-level languages (really little). Nearly one month ago I thought it would be a good idea to see how assembly worked so after choosing nasm (IA-32) on linux I started learning from a tutorial. Now, after ending it, I tried to write a simple program where you get the computer to print a list of 100 number (1 2 4 8 16...) but I couldn't even get it right. I get this output: 1PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP(continues)...
该计划是这样的:
section .text
global main
main:
mov word [num], '1'
mov ecx, 100
doubl:
push ecx ; to push the loop counter
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 1
int 0x80
sub ecx, 30h
add ecx, ecx ; shl ecx, 1
add ecx, 30h
mov [num], ecx ; deleting this line I get 11111111111111111...
pop ecx ; to pop the loop counter
loop doubl
exit:
mov eax, 1
int 0x80
section .bss
num resw 2
它看起来像错误是在双打的数量或存储在变量民的一个组成部分,但我不明白为什么会发生,以及如何解决它。
It looks like the error is in the part that doubles the number or the one that stores it in the variable 'num', yet I don't understand why it happens and how to solve it.
有人可以解释我时准确使用方括号?是否有一个规则还是什么?本教程将其称为有效地址它看起来像我不得不使用支架时,我想移动(或做的东西)变量的内容,而不是做给变量的地址。然而,我很困惑的。我看到它适用于:
By the way can someone explain me when to use the square brackets exactly? Is there a rule or something? The tutorial calls it "effective address" and it looks like I have to use the brackets when I want to move (or do something with) the content of a variable instead of doing it to the variable's address. Yet I'm quite confused about it. I see it used in:
mov ebx, count
inc word [ebx]
mov esi, value
dec byte [esi]
不过,是不是很明显,一个人想增加寄存器的内容(因为它没有一个地址(或不是吗?)??
But isn't it obvious that one wants to increment the content of the register (since it doesn't have an address (or does it?) ??
我希望有人能帮助我。预先感谢您的回答。
I hope someone can help me. Thanks in advance for your answers.
推荐答案
您数字将快速增长不仅仅是一个单一的数字更大。什么你应该做的是在 NUM
一个整数,而不是一个字符,然后将其转换整数为字符串,可以用 SYS_WRITE打印
。
Your numbers will quickly grow larger than just a single digit. What you ought to be doing is have an integer in num
rather than a character, and then convert that integer into a string that you can print with sys_write
.
下面是执行转换的一种方式:
Here's one way of doing the conversion:
; Input:
; EAX = integer value to convert
; ESI = pointer to buffer to store the string in (must have room for at least 10 bytes)
; Output:
; EAX = pointer to the first character of the generated string
int_to_string:
add esi,9
mov byte [esi],0 ; String terminator
mov ebx,10
.next_digit:
xor edx,edx ; Clear edx prior to dividing edx:eax by ebx
div ebx ; eax /= 10
add dl,'0' ; Convert the remainder to ASCII
dec esi ; store characters in reverse order
mov [esi],dl
test eax,eax
jnz .next_digit ; Repeat until eax==0
mov eax,esi
ret
您可以使用这样的:
Which you can use like this:
mov dword [num],1
...
mov eax,[num]
mov esi,buffer
call int_to_string
; eax now holds the address that you pass to sys_write
...
num resd 1
buffer resb 10
您数量加倍可以简化为 SHL DWORD [NUM],1
Your number-doubling can be simplified to shl dword [num],1
这篇关于A(应该是)简单的程序与NASM doesnt't工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!