Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 26th, 2004, 3:06 AM   #1
Lance
Programmer
 
Lance's Avatar
 
Join Date: Oct 2004
Location: Chicago, IL USA
Posts: 74
Rep Power: 4 Lance is on a distinguished road
Send a message via AIM to Lance
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

    ret

I'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";
Lance is offline   Reply With Quote
Old Dec 26th, 2004, 2:00 PM   #2
Lance
Programmer
 
Lance's Avatar
 
Join Date: Oct 2004
Location: Chicago, IL USA
Posts: 74
Rep Power: 4 Lance is on a distinguished road
Send a message via AIM to Lance
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";
Lance is offline   Reply With Quote
Old Dec 26th, 2004, 5:16 PM   #3
Lance
Programmer
 
Lance's Avatar
 
Join Date: Oct 2004
Location: Chicago, IL USA
Posts: 74
Rep Power: 4 Lance is on a distinguished road
Send a message via AIM to Lance
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

    ret

So 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";
Lance is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC