Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[NASM] how to change some codes and link?

assembly

This topic has been archived. This means that you cannot reply to this topic.
20 replies to this topic

#13 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 October 2014 - 10:25 AM

We have a policy of not doing people's homework. We'll guide you but we won't do it for you. Write out a step by step method for converting an integer string to an integer and post it when you're done. Then we can go over it and translate it to assembly code step by step.


Edited by dargueta, 08 October 2014 - 10:25 AM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#14 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 08 October 2014 - 10:37 AM

it is not my homework. my teacher doesn't know much about programming he said please tell me something about assembly and a simple program that can calculate the average of three numbers.

#15 gonerogue

gonerogue

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 197 posts

Posted 08 October 2014 - 11:08 AM

Actually, no, that just means that bl is 0. Dividing by 0 is a problem. This will happen whenever the place you're looking at contains '0'. For example, "10" will explode when you try to divide the last digit.

But dividing by zero is not the only case in which you get a floating point exception.

div bl -> the content of edx:eax is divided by the operand.

If the quotient doesn't fit in eax, you also get a floating point exception (which I believe is his case, but I didn't debugged the program to be 100% sure). He should check the content of the edx (which I don't believe is zero), eax and bl registers before the division operation is executed, to see for sure what is causing the exception.

 

For example:

 

section .text

global _start

_start:
    mov edx, 0xFFFF0000
    mov eax, 0x12345678
    mov bl, 3
    div bl

 

adrian@adrian:~$ nasm -f elf test.asm
adrian@adrian:~$ ld -m elf_i386 -s -o test test.o
adrian@adrian:~$ ./test
Floating point exception -> and bl is not 0
 


Edited by gonerogue, 08 October 2014 - 11:25 AM.


#16 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 October 2014 - 11:24 AM

div bl -> the content of edx:eax is divided by the operand.

 

This is incorrect. See the description of the DIV instruction here.


sudo rm -rf / && echo $'Sanitize your inputs!'


#17 gonerogue

gonerogue

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 197 posts

Posted 08 October 2014 - 11:29 AM

I am looking here:

http://home.myfairpo...#section-A.4.59

http://www.aldeid.co...nstructions/div

 

For DIV r/m32, EDX:EAX is divided by the given operand; the quotient is stored in EAX and the remainder in EDX.

Did I missinterpreted the description ?

 

LE: I just now realized that the operand is bl, not ebx ... don't know how I missed this ... my bad :worry:

Anyway, you can get a floating point exception without dividing by zero, see the code in my previous post.


Edited by gonerogue, 08 October 2014 - 11:43 AM.


#18 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 08 October 2014 - 11:49 AM

I fixed it

by : mov ah,0

section .bss            ;Uninitialized data
    average resb 5
 num1 resb 5
num2 resb 5
num3 resb 5
section .text

global _start                ; make the main function externally visible

_start:
       ;User prompt
       mov eax, 4
       mov ebx, 1
       mov ecx, messaging
       mov edx, lenmessaging
       int 0x80

       ;Read and store the user input
       mov eax, 3
       mov ebx, 0
       mov ecx, num1  
       mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
int 0x80
mov eax, 4
       mov ebx, 1
       mov ecx, messaging
       mov edx, lenmessaging
       int 0x80
;Read and store the user input
       mov eax, 3
       mov ebx, 0
       mov ecx, num2  
       mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
int 0x80
mov eax, 4
       mov ebx, 1
       mov ecx, userMsg
       mov edx, lenUserMsg
       int 0x80
;Read and store the user input
       mov eax, 3
       mov ebx, 0
       mov ecx, num3  
       mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
       int 0x80
       ;Output the message
       mov eax, 4
       mov ebx, 1
       mov ecx, dispMsg
       mov edx, lenDispMsg
       int 0x80  
 
              ; moving the first number to eax register and second number to ebx
    ; and subtracting ascii '0' to convert it into a decimal number
    mov eax, [num1]
    sub eax, '0'
    mov ebx, [num2]
    sub ebx, '0'

    ; add eax and ebx
    add eax, ebx
    mov ebx, [num3]
    sub ebx, '0'
        add eax, ebx

    
  	mov ah,0
	mov 	bl, '3'
	sub     bl, '0'
	div 	bl
	add eax, '0'

       mov [average], eax

       ;Output the number entered
       mov eax, 4
       mov ebx, 1
       mov ecx, average
       mov edx, 5
       int 0x80

       ;////////////////  
mov eax, 0x1              ; system call number for exit
    sub esp, 4                ; OS X (and BSD) system calls needs "extra space" on stack
    int 0x80                  ; make the system call

section  .data ;Data segment
    userMsg db 'Please enter a number: ' ;Ask the user to enter a nu
    lenUserMsg equ $-userMsg             ;The length of the message
    dispMsg db 'The average is : '
    lenDispMsg equ $-dispMsg  
messaging db 'Enter another number : '
    lenmessaging equ $-messaging
              

but now I have another problem :

Screen Shot 2014-10-08 at 11.13.43 PM.png

 

as you see there are problems about characters and what is '\n' in assembly (nasm) ?

help me :(

thanks ;)


Edited by SnakeS, 08 October 2014 - 11:53 AM.


#19 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 October 2014 - 12:14 PM

You're outputting the integer value. You're not converting the output value properly. Also, "\n" is 0x0a on *NIX systems and 0x0d, 0x0a (two bytes) on Windows.


sudo rm -rf / && echo $'Sanitize your inputs!'


#20 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 08 October 2014 - 12:28 PM

wha should I do?

#21 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 October 2014 - 12:36 PM

Separate converting from ASCII to integer and integer to ASCII into separate functions would help you debug and clean up your code.

 

Hint: You convert to an ASCII digit the opposite way of getting an integer from an ASCII digit - by adding '0'.


Edited by dargueta, 08 October 2014 - 02:22 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download