Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Code assembles and links but bash rejects it

mikfigmikfig Member Posts: 7
I just wrote this code, it assembles fine. I used GAS and it x86_64.
I linked it like this:

ld -o ./maximum /usr/lib64/crt1.o /usr/lib64/crti.o ./maximum.o -lc

There's no errors. But then I run it and I get this:

bash: ./maximum: No such file or directory

Just to make sure, I ran it and gdb and got the same error.

Here's the code:

[code]
# PURPOSE: This program finds the maximum number of a
# set of data items.
#


.section .data

hello_msg:

.asciz "Hello from GAS :)\n"

current_data_item_msg:

.asciz "Current data item #%d: %d\n"

greatest_data_item:

.asciz "Greatest data item: %d\n"

exit_msg:

.asciz "See ya!!\n"

debug_1:

.asciz "Args #: %d\n"

debug_2:

.asciz "Mem for args: %d + 8 bytes\n"

.section .text

.globl main

main:

leaq (%rsp), %rdx # need old stack frame to get args

pushq %rbp # Create a new stack frame
movq %rsp, %rbp # following C runtime rules
pushq %rsi
pushq %rdi
pushq %rbx

movq (%rdx), %rcx # Move argc to counter



pushq %rdx # START DEBUG: Print # args
pushq %rcx

pushq %rcx
pushq $debug_1
call printf

addq $16, %rsp

popq %rcx
popq %rdx # END DEBUG




cmpq $2, %rcx # if no arguments, goto no_args
jl no_args

decq %rcx
movq %rdx, %rsi
addq $8, %rsi # skip the program name, store start of args in rsi

movq %rcx, %rax # add enough memory to the stack frame
movq $8, %rbx
mulq %rbx # to hold all the args
movq %rax, %rbx
addq $8, %rbx # add extra 8 bytes for terminating data item (0)



pushq %rdx # START DEBUG: Print mem needed for args on stack
pushq %rbx
pushq %rcx

subq $8, %rbx

pushq %rbx
pushq $debug_2
call printf

addq $16, %rsp
popq %rcx
popq %rbx
popq %rdx # END DEBUG



movq %rsp, %rdi # Make room on stack for args
subq %rbx, %rsp # put starting address of args in %rdi

movq $0, %rax # Going to store args source index in %rax

#
# Go through argv[ i ] and put every argv on the stack
#
# Or if there are no argvs, then just use random numbers
#

# rcx: decrementing counter on number of args left
# rdi: start of stack space for storing args
# rsi: start of pointers to arg strings
# rax: current arg number

arg_loop_start:

cmpq $0, %rcx
je arg_quit

pushq %rcx

movq %rax, %r9
pushq %r9

pushq 0(%rsi,%rax,8)
call atoi
addq $8, %rsp

popq %r9

movq %rax, 0(%rdi,%r9,8) # put current arg in stack

movq %r9, %rax

popq %rcx

incq %rax
decq %rcx

no_args:

movq %rsp, %rdi
subq $40, %rsp # Make room for 5 random data items

pushq $0 # Seed random number gen
call time
addq $8, %rsp

pushq %rax
call srand
addq $8, %rsp

movq $5, %rcx # Use 5 random data items
movq $0, %rax

no_args_loop_start:

cmpq $0, %rcx
je arg_quit

movq %rax, %r9

pushq %r9

call rand
addq $1, %rax # Make sure its not 0

popq %r9

movq %rax, 0(%rdi, %r9, 8)

movq %r9, %rax

incq %rax
decq %rcx

arg_quit:

movq $0, 0(%rdi, %rax, 8) # Add the terminating zero


pushq $hello_msg
call printf
addq $8, %rsp


movq $0, %rcx # Put 0 in the data item index
movq (%rdi), %rbx # Put the first number in ebx as the largest item

start_data_loop:
movq 0(%rdi, %rcx, 4), %rdx

pushq %rcx
pushq %rdx

pushq %rdx
pushq %rcx
pushq $current_data_item_msg
call printf

addq $24, %rsp

popq %rdx
popq %rcx

cmpq $0, %rdx
je data_loop_quit

cmpq %rdx, %rbx
jle not_larger

movq %rdx, %rbx

not_larger:
incq %rcx

data_loop_quit:

pushq %rdx
pushq $greatest_data_item
call printf

addq $16, %rsp

pushq $exit_msg
call printf

sub $8, %rsp


popq %rbx # Destroy the stack frame and exit
popq %rdi
popq %rsi
movq %rbp, %rsp
popq %rbp

ret
[/code]

Btw, I'm just starting to learn assembly so I'm guessing this code doesn't look to good :P

In fact, I'm pretty sure I used mulq wrong here:

[b]movq %rcx, %rax # add enough memory to the stack frame
movq $8, %rbx
mulq %rbx [/b] # to hold all the args
movq %rax, %rbx
addq $8, %rbx # add extra 8 bytes for terminating data item (0)

Thanks,
mikfig
Sign In or Register to comment.