Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Assembly code

  1. Apr 17, 2006 #1
    Can anyone explain how this simple assembly code is working:
    This is specifically for pentium ones

    I am actually lacking codes for assembly,,didn;t get any...if you have any please give..
    ----------------------------------------------------------------
    #include<asm/unistd.h>
    #include<syscall.h>




    .data
    buffer: .int 0
    fileread:
    .string "fileread.S"
    filewrite:
    .string "filewrite.S"






    .text
    .globl _start
    _start:
    movl $(SYS_open), %eax
    movl $(fileread), %ebx
    movl $(0) ,%ecx
    int $0x80

    push %eax

    movl $(SYS_open), %eax
    movl $(filewrite), %ebx
    movl $(1), %ecx
    int $0x80



    push %eax
    c1:
    movl $(SYS_read) ,%eax
    movl 4(%esp), %ebx
    movl $buffer, %ecx
    movl $4 ,%edx
    int $0x80






    cmp $0, %eax
    jle end

    movl $(SYS_write) ,%eax
    movl (%esp),%ebx
    movl $buffer,%ecx
    movl $4,%edx
    int $0x80
    jmp c1

    end:

    addl $8, %esp
    movl $(SYS_exit),%eax
    xorl %ebx,%ebx
    int $0x80
    ret
     
  2. jcsd
  3. Apr 18, 2006 #2

    -Job-

    User Avatar
    Science Advisor

    It looks as if it is copying a file to another. I'm not familiar with this specific assembly language but from my assembly experience then i assume:
    . main memory locations are preceded by $
    . registers are preceded by %
    . movl A, B moves the content in A to B, not sure how data is addressed, maybe by word or word*2, but doesn't matter much i guess.
    . push would push something onto the stack. typically you use this to backup your registers so that you can reuse them without overwriting data
    . labels are followed by ":". So c1: ... is a line labeled "c1"
    . jmp clearly jumps to a line of code
    . jle probably is "jump if less than or equal to"
    . cmp A, B, compares A and B and stores 1 or 0 in a specific register depending on whether A<=B or not, this is the value used by jle, i think
    . %0 should be the 0 register which is the register storing the value 0

    If i'm not mistaken in the above definitions then the first code block:
    Code (Text):

    _start:
    movl $(SYS_open), %eax
    movl $(fileread), %ebx
    movl $(0) ,%ecx
    int $0x80

    push %eax

    movl $(SYS_open), %eax
    movl $(filewrite), %ebx
    movl $(1), %ecx
    int $0x80
    ... opens an input file and saves the location of the first word in register %ecx. It then opens the file it will write to and saves the location of the first word in that file (the first word we'll write data to).
    Without getting into specifics it looks like the following code establishes and populates a buffer which will be used in reading from the file:
    Code (Text):
    push %eax
    c1:
    movl $(SYS_read) ,%eax
    movl 4(%esp), %ebx
    movl $buffer, %ecx
    movl $4 ,%edx
    int $0x80
     
    The next block of code clearly is a loop. We use cmp to compare the word we just read from the input file with 0. If it's 0 then it's an EOF (end of file) and so we exit the loop by jumping to the line labeled "end". If it's not the EOF, then we write the contents of the buffer to the output file and we jump to the beginning of the loop at the line "jmp c1" and follow the process all over again, until the whole input file is read:
    Code (Text):
    cmp $0, %eax
    jle end

    movl $(SYS_write) ,%eax
    movl (%esp),%ebx
    movl $buffer,%ecx
    movl $4,%edx
    int $0x80
    jmp c1
    The remaining block of code just seems to clean everything up and exit the program:
    Code (Text):
    end:

    addl $8, %esp
    movl $(SYS_exit),%eax
    xorl %ebx,%ebx
    int $0x80
    ret
    So it looks very much like this assembly program creates a copy of a file.
     
  4. Apr 21, 2006 #3
    -Job- has pretty much covered what is going on. I just thought I'd explain that each block ending with 'int $0x80' is calling a C style system call. 'int $0x80' itself generates a software interrupt giving control to the (I assume Linux) kernel. If you look at the file /usr/include/asm/unistd.h (assuming you are on a UNIX system) you will see a list of system call names along with a corresponding number. These system calls are C style functions, and the kernel takes function parameters from registers. You can look up the details and function parameters of these system calls using the 'man' command, i.e. 'man 2 open' gives you information about the open() function (note that most of the programmer man pages are on the second page, hence the 2).

    The above line basically moves the number corresponding to the open system call (checking my unistd.h, it's number 5) into the %eax. When calling system functions in assembly, the call number must go into register %eax.

    Checking the 'man 2 open' page the function prototype is:
    The first parameter is 'const char *pathname' this corresponds to the 'fileread' string. As it is the first parameter it must be stored in %ebx:
    Again from the prototype, the second parameter is 'int flags' and this must be stored in register %ecx (basically parameters must be stored in the same order as the protoype). Check the manpage for information on what 'flags' does.
    The final line of each block is:
    Again this calls the kernel which looks at the registers and executes the system call for you.

    I'll leave it as an exercise to check the manpages for the other calls and figure out exactly what's going on.

    rhinovirus
     
  5. Apr 21, 2006 #4

    -Job-

    User Avatar
    Science Advisor

    Welcome around Rhino.
     
  6. Apr 22, 2006 #5
    Thank you -Job-.

    rhinovirus
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?