Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Assembler - standard input and conversion

assembly conversion hex

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

#1 cryuff

cryuff

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 13 April 2015 - 07:58 AM

I want my program to read characters from standard input and summed it up in kodd.

However, when I entered, eg. The value of 23, i saved the following ASCII value of the first character only, ie. 2 without 3

The second problem is how to convert the value stored in kodd to hex code? Also how to initialize esi and edi properly I think about mov $kod, %edi but what about esi?

data
        kod: .space 1000
        kodd: .long 0
        kod_roz = 10000

.text

        text: .ascii "Podaj ciag znakow zakoncz enterem"
        text_len = . -text




.align 32

SYSEXIT=1
SYSREAD=3
SYSWRITE=4
STDOUT=1
STDIN=0
EXIT_SUCCESS=0

.global _start

_start:
        mov $4, %eax
        mov $1, %ebx
        mov $text, %ecx
        mov $text_len, %edx
        int $0x80
s:
        mov $3, %eax
        mov $0, %ebx
        mov $kod, %ecx
        mov $kod_roz,  %edx
        int $0x80
        mov kod(%esi,1),%bl #poczatek
        cmp $'\n',%bl
        je koniec
petla:
        mov kod(%edi,1),%bl     #koniec
        add %ebx, kodd
        je s


fm:
        inc %edi
        jmp s
koniec:
        mov $1,%eax
        mov $0,%ebx
        int $0x80


#2 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 13 April 2015 - 12:05 PM

I want to help u but I dont know a lot about system call in linux but u can use standard c library function like printf and scanf I've code this for u try to compile it in nasm :

nasm -f elf 1.asm
gcc -o 1 1.0
./1 
extern printf ,scanf
section .data 
msg : db "Enter value of a ",10,0
A : times 4 db 0
typ : db "%s",0
typ1 : db "%d",0
section .text
global main
main:
push ebp
mov ebp, esp
push msg
push typ
call printf
add esp ,8

xor eax , eax
xor ebx , ebx
push A
push typ1
call scanf
mov ebx,[A]
add esp ,8
push ebx
push typ1
call printf
add esp ,8
mov esp , ebp
pop ebp
ret

Edited by zika, 13 April 2015 - 12:11 PM.


#3 cryuff

cryuff

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 13 April 2015 - 01:54 PM

If you can add comments it would be great. I don't understand this syntax ;(



#4 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 13 April 2015 - 02:59 PM

extern _printf ,_scanf ; printf its output and scanf is the input 
;in c we can use printf like this // printf("\n Hello world");
;and scanf like this // scanf("%d",&A);  
section .data 
msg : db "Enter value of a ",10,0 ; this our msg "Enter value of a" and 10 for "\n" which mean new line and 0 indicate the end of string
A : times 4 db 0; this our stored value 
typ : db "%s",0 ; string
typ1 : db "%d",0; decimal
section .text
global main
main:
push ebp   ; push ebp to stack to reserve it
mov ebp, esp ; copy esp to ebp
;now let's start to call printf which responsible for print 
;we say in printf it use like this   printf("\n Hello world");
;but in assembley convenction is right to left 
;so it will be like this msg then type of string["%s"] then call printf
push msg  ; 1st parameter  "Enter value of a "
push typ ; 2nd parameter "%s"
call printf ; call printf
add esp ,8 ; to remove msg and typ from the stack 
xor eax , eax ; to make eax =0
xor ebx , ebx; to make ebx =0
push A  ; 1st parameter to store value in     
push typ1 ; type of value 
call scanf ; call scanf
mov ebx,[A] ; copy value that stored in A to ebx 
push ebx ; 1st parameter  the value we want to be the output
push typ1 ; 2nd parameter type of our value 
call printf ; call printf
mov esp , ebp 
pop ebp
ret

extern _printf ,_scanf ; printf its output and scanf is the input 
;in c we can use printf like this // printf("\n Hello world");
;and scanf like this // scanf("%d",&A);  
section .data 
msg : db "Enter value of a ",10,0 ; this our msg "Enter value of a" and 10 for "\n" which mean new line and 0 indicate the end of string
A : times 4 db 0; this our stored value 
typ : db "%s",0 ; string
typ1 : db "%d",0; decimal
section .text
global main
main:
push ebp   ; push ebp to stack to reserve it
mov ebp, esp ; copy esp to ebp
;now let's start to call printf which responsible for print 
;we say in printf it use like this   printf("\n Hello world");
;but in assembley convenction is right to left 
;so it will be like this msg then type of string["%s"] then call printf
push msg  ; 1st parameter  "Enter value of a "
push typ ; 2nd parameter "%s"
call printf ; call printf
add esp ,8 ; to remove msg and typ from the stack 
xor eax , eax ; to make eax =0
xor ebx , ebx; to make ebx =0
push A  ; 1st parameter to store value in     
push typ1 ; type of value 
call scanf ; call scanf
mov ebx,[A] ; copy value that stored in A to ebx 
push ebx ; 1st parameter  the value we want to be the output
push typ1 ; 2nd parameter type of our value 
call printf ; call printf
mov esp , ebp 
pop ebp
ret


#5 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 13 April 2015 - 03:24 PM

its call INTEL syntax 

mov $4 ,%eax ; mov eax , 4


#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 14 April 2015 - 09:28 AM

I find Intel syntax is much easier to read than AT&T. That aside:

 

1) zika, I don't recommend this because your code will not work on a 64-bit system. Here's why.

2) cryuff: You can only use eax, ecx, and edx as you please; any other registers you have to save on the stack before using and restore before returning, otherwise you can crash your program.

 

That said, to convert kod into an integer is relatively straightforward:

xor     eax, eax        ; EAX = 0 - this is your sum
mov     edx, kod        ; EDX = address of kod
conversion_loop:
    movzx   ecx, BYTE [edx]     ; ECX = kod[edx]
    cmp     cl, '\n'
    je      done_converting

    ; Multiply EAX by 10 before adding the next digit
    lea     eax, [eax + eax*4]  ; EAX *= 5
    shl     eax, 2              ; EAX *= 2    
    add     eax, ecx            ; Add next digit
    inc     edx                 ; Move on to the next character in kod
    jmp     conversion_loop
    
done_converting:
    ; EAX now contains the number entered at the terminal.
    ; THIS WILL NOT WORK FOR NEGATIVE NUMBERS.


Edited by dargueta, 14 April 2015 - 09:28 AM.

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


#7 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 14 April 2015 - 09:36 AM

 

 

1) zika, I don't recommend this because your code will not work on a 64-bit system. Here's why.


HI Mr dargueta I've compiled in win 7 64 bit and its work like magic for me I'll try to run it in linux 64bit and give u my resault 



#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 14 April 2015 - 05:18 PM

I tried it and it segfaulted on me.  :scared:


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


#9 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 15 April 2015 - 06:14 AM

I tried it and it segfaulted on me.  :scared:

em , in win 7 64bit or in linux 64 bit ??



#10 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 15 April 2015 - 06:32 AM

here's on win7 64 bit machine

extern _printf ,_scanf
section .data 
msg : db "Enter value of a ",10,0
A : times 4 db 0
typ : db "%s",0
typ1 : db "%d",0
section .text
global _main
_main:
push ebp
mov ebp, esp
push msg
push typ
call _printf
add esp ,8

xor eax , eax
xor ebx , ebx
push A
push typ1
call _scanf
mov ebx,[A]
add esp ,8
push ebx
push typ1
call _printf
add esp ,8
mov esp , ebp
pop ebp
ret

NiinPYV.jpg


Edited by zika, 15 April 2015 - 06:33 AM.


#11 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 17 April 2015 - 02:55 PM

Interesting. I guess it's a *NIX thing. Or I'm being stupid and did something wrong.  :biggrin:


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


#12 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 18 April 2015 - 04:30 AM

I think 64bit assembley doesn't work in 32bit machine I'll try to make 64 bit code and see the resualt , right now I'm kind of busy cuz my car is broken when I fix it I'll try to code it see u later Mr dargueta