| Thread Closed |
Assembly code |
Share Thread | Thread Tools |
| Apr17-06, 08:28 AM | #1 |
|
|
Assembly code
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 |
| Apr18-06, 04:58 PM | #2 |
|
Recognitions:
|
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:
_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 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:
push %eax c1: movl $(SYS_read) ,%eax movl 4(%esp), %ebx movl $buffer, %ecx movl $4 ,%edx int $0x80 Code:
cmp $0, %eax jle end movl $(SYS_write) ,%eax movl (%esp),%ebx movl $buffer,%ecx movl $4,%edx int $0x80 jmp c1 Code:
end: addl $8, %esp movl $(SYS_exit),%eax xorl %ebx,%ebx int $0x80 ret |
| Apr21-06, 06:08 PM | #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).
Checking the 'man 2 open' page the function prototype is: I'll leave it as an exercise to check the manpages for the other calls and figure out exactly what's going on. rhinovirus |
| Apr21-06, 07:31 PM | #4 |
|
Recognitions:
|
Assembly code
Welcome around Rhino.
|
| Apr22-06, 08:14 AM | #5 |
|
|
rhinovirus |
| Thread Closed |
| Thread Tools | |
Similar Threads for: Assembly code
|
||||
| Thread | Forum | Replies | ||
| math operation in assembly code | Programming & Comp Sci | 2 | ||
| help in assembly | Programming & Comp Sci | 6 | ||
| Convert latex code to fortran code? | Math & Science Software | 1 | ||
| 68000 Assembly code | Introductory Physics Homework | 0 | ||
| Assembly | Electrical Engineering | 1 | ||