![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
So I'm fairly new at Assembly, but I get the jist of it. I'm trying to learn AT&T syntax, and the sort, but it's proving a bit difficult to adjust to.
This is supposed to be an infinate loop incrementation, that just prints the numbers as they increment. That's it. Can anyone point out where I went wrong? All criticism and tips would be great, as long as their is an explaination why I did it wrong. ![]() .bss
count:
.long 0
.text
.globl main
main:
movl $5,%eax
movl $1,%ebx
repeat:
movl $count,%ecx
movl $count,%edx
int $0x80
incl count
jmp repeat
retI'm pretty sure using the BSS segment as a var for a simple incrementation is stupid, but I'm trying to figure out how using variables really works. I've tried this with the %esp and #esi register just to see if my usage of $count is wrong, but it turns out the same. Any help would be nice. ![]()
__________________
/* LANCE */ C++; /* this makes C bigger but returns the old value */ char *site = "slackwise.net", *home = "lance.slackwise.net", *pics = "flickr.com/photos/slackwise"; |
|
|
|
|
|
#2 |
|
Programmer
|
Well that was stupid... I was using syscall 5 (open) the entire time, when I meant 4 (write). Either way, it still doesn't work.
.section .bss buf: * * * *.long 0 .section .text .globl main main: * * * *movl * *$4,%eax * * * *movl * *$1,%ebx * * * *jmp loop loop: * * * *movl * *$buf,%ecx * * * *movl * *$1,%edx * * * *int * * $0x80 * * * *incl * *buf * * * *jmp * * loop * * * *ret If I remove loop and just jmp main it starts printing random garbage: 123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Over and over, until i ^C'ed it. So then I wrote it in C... #include <stdio.h>
int i;
int main()
{
while (1)
write(1, i++, 1);
return 0;
}...and ran gcc -S -O0 -o inc_c.s inc.c on it to see the assembly: .file "inc.c"
.text
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
.L2:
subl $4, %esp
pushl $1
movl i, %eax
pushl %eax
incl i
pushl $1
call write
addl $16, %esp
jmp .L2
.Lfe1:
.size main,.Lfe1-main
.comm i,4,4
.ident "GCC: (GNU) 3.2.2"Any help here? It apparently stores 'i' in an... automatic variable? I'm a bit confused...
__________________
/* LANCE */ C++; /* this makes C bigger but returns the old value */ char *site = "slackwise.net", *home = "lance.slackwise.net", *pics = "flickr.com/photos/slackwise"; |
|
|
|
|
|
#3 |
|
Programmer
|
Well, so I did it. But it doesn't print just numbers, but text. I forgot write() prints ASCII only.
![]() .bss
.comm buf,1,16
.text
.globl main
main:
movl $4,%eax
movl $1,%ebx
movl $buf,%ecx
movl $1,%edx
int $0x80
incl buf
jmp main
retSo I guess that's as far as I can go without forcing output in the form of the ASCII codes for numbers. :/ If you have a faster way to write this, just tell me. I ponder writing it with the stack instead of common data instead. That would probably be better, but I don't know.
__________________
/* LANCE */ C++; /* this makes C bigger but returns the old value */ char *site = "slackwise.net", *home = "lance.slackwise.net", *pics = "flickr.com/photos/slackwise"; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|