How Does This Assembly Code Work for Pentium One?

  • Thread starter Thread starter heman
  • Start date Start date
  • Tags Tags
    Assembly Code
Click For Summary

Discussion Overview

The discussion revolves around understanding a specific assembly code snippet designed for the Pentium One architecture. Participants explore how the code functions, particularly in the context of file operations such as reading from and writing to files. The conversation includes technical explanations and interpretations of the assembly language used.

Discussion Character

  • Technical explanation
  • Exploratory
  • Conceptual clarification

Main Points Raised

  • One participant requests clarification on how the assembly code operates, particularly noting a lack of familiarity with assembly programming.
  • Another participant interprets the code as a file copying mechanism, explaining various assembly language conventions such as the use of registers and system calls.
  • A third participant elaborates on the role of the 'int $0x80' instruction, indicating it invokes system calls in a UNIX-like environment and describes how parameters are passed through registers.
  • Participants discuss the significance of specific lines of code, such as how system call numbers are loaded into registers and how file paths and flags are specified for the 'open' system call.
  • There is acknowledgment of the need to refer to system call documentation for deeper understanding, particularly regarding the parameters and behavior of the system calls used in the code.

Areas of Agreement / Disagreement

Participants generally agree on the overall function of the assembly code as a file copying program, but there are variations in their interpretations of specific details and conventions used in assembly language. The discussion remains exploratory without a definitive consensus on all aspects of the code.

Contextual Notes

Some assumptions about the assembly language syntax and system call behavior are not explicitly defined, and participants rely on their interpretations and experiences. There is also a dependence on external documentation for full clarity on system calls.

Who May Find This Useful

This discussion may be useful for individuals interested in assembly programming, particularly those working with the Pentium architecture or exploring file operations in a UNIX-like environment.

heman
Messages
353
Reaction score
0
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
 
Technology news on Phys.org
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

... 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:
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:
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:
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.
 
-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).

movl $(SYS_open), %eax
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:
int open(const char *pathname, int flags);

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:
movl $(fileread), %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.
movl $(0) ,%ecx

The final line of each block is:
int $0x80
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
 
Welcome around Rhino.
 
-Job- said:
Welcome around Rhino.

Thank you -Job-.

rhinovirus
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 6 ·
Replies
6
Views
6K