Sponsored Content
Full Discussion: Understanding Assembly Code
Top Forums Programming Understanding Assembly Code Post 302931995 by Azrael on Sunday 18th of January 2015 01:04:54 AM
Old 01-18-2015
Understanding Assembly Code

As the title suggests, I want to better understand the following assembly code:

Code:
section .text
    global main          ; must be declared for linker (gcc)
main:                        ; tell linker entry point
    mov    edx, len     ; message length
    mov    ecx, msg    ; message to write
    mov    ebx, 1         ; file descriptor (stdout)
    mov    eax, 4         ; syscall for write (sys_write)
    int      0x80           ; call kernel

    mov    edx, 9        ; message length
    mov    ecx, s2       ; message to write
    mov    ebx, 1        ; file descriptor (stdout)
    mov    eax, 4        ; syscall number for write (sys_write)
    int      0x80          ; call kernel
    mov    eax, 1        ; system call (sys_exit)
    int      0x80          ; call kernel

section .data
msg db 'Displaying 9 stars', 0xa ; a message
len equ $ - msg            ; length of message
s2 times 9 db '*'

As you can see I already have descriptions in the comments from the tutorial I found here. Here are some of the things I don't understand:

1. - What is s2? Is this just a variable or a register I know nothing about?

2. When '1' is moved into ebx, is this a parameter to the sys_exit later called in eax? I found a listing of Linux syscalls here and it does seem sys_exit does take one parameter in ebx that's an integer. If this is the case why not exit cleanly with zero?

I just want to make sure I understand everything correctly in this. Thanks in advance!
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Understanding Code in IF LOOP

Hello All, I would like to know and understand the difference between the below 3 IF loops and also if possible what are the different other parameters i could use other than those mentioed in the below lF LOOP conditions, appreciate your help. Thanks, Sam. (1 Reply)
Discussion started by: Ariean
1 Replies

2. UNIX Desktop Questions & Answers

Understanding the code

hello all, May i know what is this "DEBUG_ME $DEBUG_CMD main" doing in the below code. I am confused with alias also "alias DEBUG_ME='#'". Thanks for your help. set -x alias DEBUG_ME='#' if ; then . /product/apps/informatica/v7/pc/ExtProc/debug.ksh "$1" fi # Declaring the... (1 Reply)
Discussion started by: Ariean
1 Replies

3. Programming

Need assembly code for C program

Dear Buddies, I need assembly code for a compiled c program in unix. Kindly help me.... Thanking you in advance. (1 Reply)
Discussion started by: karthik537
1 Replies

4. Programming

Help with assembly code

I want make simple assembly code for some thing like this a^6+6a^2+2a and range of a is between -3 to 3. I tried but it is not working properly. As this is my first assembly program that I am going to try, I want some help with it. I found this example online but i dont want this kind of... (2 Replies)
Discussion started by: Learnerabc
2 Replies

5. Shell Programming and Scripting

Help understanding Perl code.

Well, I found myself trying to fix some Perl code (Ive never done any Perl in my life) and I pinpointed the place where the bug could be. But to be sure I have to know what does a few line of code mean: $files_lim =~ (/^\d*$/) $files_lim =~ (/^\d*h$/)$files_age =~ s/h//The code where this was... (0 Replies)
Discussion started by: RedSpyder
0 Replies

6. Shell Programming and Scripting

Help understanding some Perl code.

Well, I found myself trying to fix some Perl code (Ive never done any Perl in my life) and I pinpointed the place where the bug could be. But to be sure I have to know what does a few line of code mean: $files_lim =~ (/^\d*$/) $files_lim =~ (/^\d*h$/) $files_age =~ s/h// The code where... (2 Replies)
Discussion started by: RedSpyder
2 Replies

7. Programming

Understanding perl code

What is the difference between the two statements below? A: $a->{"$fruit"}->{"$color"}->{size} = $size B: $size = $a->{"$fruit"}->{"$color"}->{size} Please assist. Thanks! (0 Replies)
Discussion started by: onlinelearner02
0 Replies

8. Shell Programming and Scripting

auto-generating assembly code by variables found by script

Hi everybody I'm working on a list of registers(flip-flops to be exact), now i need to extract some value from this list and use them as arguments to pass them to some assembly code for example i have: 118 chain79 MASTER (FF-LE) FFFF 1975829 /TCK F FD1TQHVTT1 ... (1 Reply)
Discussion started by: Behrouzx77
1 Replies

9. Shell Programming and Scripting

Help with perl code understanding

Hi, I need to understand below perl code, can some one advise me. perl -MDate::Parse -e'BEGIN{$main::now=time;$main::old=(time-60*30)}' -nE'if(/^(\w+\s+\d+\s+\d+:\d+:\d+)/) {$t=str2time $1; $t > $old && $t < $now && print}' (1 Reply)
Discussion started by: learnbash
1 Replies

10. Shell Programming and Scripting

[Solved] Help understanding this code!!

Hi guys, I am still learning awk and much apprecated to shed some light on the following: the questions asked is below! { total = i = 0 do { ++i total += $i } while ( total <= 100 ) print i, ":", total } File used: cat test.do 45 25 60 20 10 105 50 40 33 5 9 67 108 3 5 4 (2 Replies)
Discussion started by: Apollo
2 Replies
SYSCALL(2)						     Linux Programmer's Manual							SYSCALL(2)

NAME
syscall - indirect system call SYNOPSIS
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <unistd.h> #include <sys/syscall.h> /* For SYS_xxx definitions */ int syscall(int number, ...); DESCRIPTION
syscall() is a small library function that invokes the system call whose assembly language interface has the specified number with the specified arguments. Employing syscall() is useful, for example, when invoking a system call that has no wrapper function in the C library. syscall() saves CPU registers before making the system call, restores the registers upon return from the system call, and stores any error code returned by the system call in errno(3) if an error occurs. Symbolic constants for system call numbers can be found in the header file <sys/syscall.h>. RETURN VALUE
The return value is defined by the system call being invoked. In general, a 0 return value indicates success. A -1 return value indicates an error, and an error code is stored in errno. NOTES
syscall() first appeared in 4BSD. Architecture-specific requirements Each architecture ABI has its own requirements on how system call arguments are passed to the kernel. For system calls that have a glibc wrapper (e.g., most system calls), glibc handles the details of copying arguments to the right registers in a manner suitable for the architecture. However, when using syscall() to make a system call, the caller might need to handle architecture-dependent details; this requirement is most commonly encountered on certain 32-bit architectures. For example, on the ARM architecture Embedded ABI (EABI), a 64-bit value (e.g., long long) must be aligned to an even register pair. Thus, using syscall() instead of the wrapper provided by glibc, the readahead() system call would be invoked as follows on the ARM architecture with the EABI: syscall(SYS_readahead, fd, 0, (unsigned int) (offset >> 32), (unsigned int) (offset & 0xFFFFFFFF), count); Since the offset argument is 64 bits, and the first argument (fd) is passed in r0, the caller must manually split and align the 64-bit value so that it is passed in the r2/r3 register pair. That means inserting a dummy value into r1 (the second argument of 0). Similar issues can occur on MIPS with the O32 ABI, on PowerPC with the 32-bit ABI, and on Xtensa. The affected system calls are fadvise64_64(2), ftruncate64(2), posix_fadvise(2), pread64(2), pwrite64(2), readahead(2), sync_file_range(2), and truncate64(2). Architecture calling conventions Every architecture has its own way of invoking and passing arguments to the kernel. The details for various architectures are listed in the two tables below. The first table lists the instruction used to transition to kernel mode, (which might not be the fastest or best way to transition to the kernel, so you might have to refer to the VDSO), the register used to indicate the system call number, and the register used to return the system call result. arch/ABI instruction syscall # retval Notes ----------------------------------------------------------------------------------- arm/OABI swi NR - a1 NR is syscall # arm/EABI swi 0x0 r7 r1 blackfin excpt 0x0 P0 R0 i386 int $0x80 eax eax ia64 break 0x100000 r15 r10/r8 parisc ble 0x100(%sr2, %r0) r20 r28 s390 svc 0 r1 r2 NR may be passed directly with s390x svc 0 r1 r2 "svc NR" if NR is less than 256 sparc/32 t 0x10 g1 o0 sparc/64 t 0x6d g1 o0 x86_64 syscall rax rax The second table shows the registers used to pass the system call arguments. arch/ABI arg1 arg2 arg3 arg4 arg5 arg6 arg7 ---------------------------------------------------------- arm/OABI a1 a2 a3 a4 v1 v2 v3 arm/EABI r1 r2 r3 r4 r5 r6 r7 blackfin R0 R1 R2 R3 R4 R5 - i386 ebx ecx edx esi edi ebp - ia64 r11 r9 r10 r14 r15 r13 - parisc r26 r25 r24 r23 r22 r21 - s390 r2 r3 r4 r5 r6 r7 - s390x r2 r3 r4 r5 r6 r7 - sparc/32 o0 o1 o2 o3 o4 o5 - sparc/64 o0 o1 o2 o3 o4 o5 - x86_64 rdi rsi rdx r10 r8 r9 - Note that these tables don't cover the entire calling convention--some architectures may indiscriminately clobber other registers not listed here. EXAMPLE
#define _GNU_SOURCE #include <unistd.h> #include <sys/syscall.h> #include <sys/types.h> int main(int argc, char *argv[]) { pid_t tid; tid = syscall(SYS_gettid); tid = syscall(SYS_tgkill, getpid(), tid); } SEE ALSO
_syscall(2), intro(2), syscalls(2) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2013-06-21 SYSCALL(2)
All times are GMT -4. The time now is 02:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy