Find the answer to your Linux question:
Results 1 to 7 of 7
So I've been toying with assembly, and I decided to take a break from my reading to try to make something completely on my own. It returns the inverse of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Location
    Alaska
    Posts
    55

    Assembly (simple code issue)


    So I've been toying with assembly, and I decided to take a break from my reading to try to make something completely on my own.
    It returns the inverse of a number, except for some bizarre reason, when I tell it to print the string prompt, it prints prompt and result, then result again after the inverse is found.

    Help anybody?

    Oh yeah, I'm using Dr. Paul Carter's assembly tutorial, and I was supposed to download some include files. A quick google search can get you the site.

    Code:
    %include "inc/asm_io.inc"
    segment .data
    prompt db  "What do you want to inverse? "
    outmsg db  "The inverse of your number is "
    
    segment .bss
    input   resd 1
    segment .text
    	global asm_main
    asm_main:
    	enter 0,0
    	pusha
    	mov eax,prompt   ;; eax = "enter the number"
    	call print_string    ;; prints the value of eax
    	call read_int        ;; gets int from user and puts in eax
    	mov ebx,eax
    	not ebx              ;; inverse command
    	mov eax,outmsg  ;; "inverse of number is"
    	call print_string   ;;  ^^^^^^^^^^^^
    	mov eax,ebx
    	call print_int       ;; Prints the number
    	call print_nl
    	popa
    	mov eax,0
    	leave
    	ret
    nasm -f elf inverse.asm
    gcc -o inverse inverse.o inc/driver.c inc/asm_io.o

    Processor 80x86
    Last edited by MisterDood; 06-30-2011 at 05:09 PM. Reason: Commented code

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,753
    You don't show the code for the call targets, print_string, pinrt_int, etc.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Location
    Alaska
    Posts
    55
    Sorry for the late reply, I've been busy working lately.

    Anyways, I thought that when I called print_string, it printed the value of eax by default. How would I go about adding another target to that? I'm still very new to this, so please forgive my lack of knowledge. Also, thank you.

    P.S. I've realized lately that people who help others don't get enough credit these days, or maybe they never did really. Without help forums like these, Linux probably wouldn't have at least 90% of its current users.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    42
    Quote Originally Posted by MisterDood View Post
    Anyways, I thought that when I called print_string, it printed the value of eax by default.
    No, that's what print_int does. print_string assumes that EAX contains an address and prints whatever it finds there until it reaches a zero.
    You forgot to terminate the strings.

  6. #5
    Just Joined!
    Join Date
    Nov 2010
    Location
    Alaska
    Posts
    55
    That you very much kind sir. That's something my tutorial failed to mention (Or maybe something I failed to remember).

  7. #6
    Just Joined!
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    42
    I looked at the tutorial and it is explained on page 17.

    One more thing:
    mov eax,prompt ;; eax = "enter the number"
    Your comment doesn't make sense here. This instruction loads the address of the label "prompt" into eax, which is what you want in this case. If you want to load the data on that address, you should use mov eax,[prompt] instead.
    Since eax is 32 bits long, this would only load the word "What" into eax, not the whole string. That's why print_string expects you to call it with the address of the string in eax.

  8. #7
    Just Joined!
    Join Date
    Nov 2010
    Location
    Alaska
    Posts
    55
    That makes a lot more sense. Thank you. I should go back and re-read the first chapter since my reading skills have obviously failed me. I also have a few questions about the use of anding (other than counting bits) and how the stack is used (though I know how the stack works thanks to MTG xD), but I'll ask those another time. In the mean time, I'll be re-reading this tutorial. Thank you all for your help and patience. Time to hit the books (or PDFs) before bed.

    P.S. My inversing program works now that I terminated the strings ( bla , 0 ). Just thought I'd relieve everyone of the suspense.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •