News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Input and output integer

Started by sage1130, April 02, 2011, 02:22:13 AM

Previous topic - Next topic

sage1130

I want to write an assembly language program for masm32 assembler which will read an integer and print the same in linux.
The program should keep reading integers until a particular integer say 50 occurs.
When 50 occurs program should not print it and exit.
But I don't know how to input integer.
Can someone explain and give the complete asm program?

Vortex

Hi sage1130,

Here is a quick example for you. It's not for the Linux environment and you will have to improve the code. Also, keep in mind the the NO HOMEWORK rule of the forum.


include GetNumb.inc


printX MACRO buffer:REQ,_format:REQ,args:VARARG

    invoke  wsprintf,buffer,_format,args
    invoke  StdOut,buffer
   
ENDM


.data

message     db 'Please type a number ',13,10,0
format1     db 13,10,'The number is %u',13,10,13,10,0


.data?

number      db NUMB_BUFFER_LEN dup(?)
buffer      db 64 dup(?)


.code

start:

    call    main
    invoke  ExitProcess,0

main PROC USES esi

    mov     esi,OFFSET number

_loop:

    invoke  StdOut,ADDR message
    invoke  StdIn,esi,NUMB_BUFFER_LEN

    mov     BYTE PTR [esi+eax-2],0  ; remove the CR+LF pair

    invoke  atodw,esi               ; convert to DWORD

    cmp     eax,50
    jne     @f
    ret
@@:
    printX  ADDR buffer,ADDR format1,eax
    je      _loop
   
    ret

main ENDP

END start



sage1130

@Vortex Thanks you very much. :bg
here is what i was looking for
; Author : Gareth Adams

SECTION .text
global _start
_start:
   call readchar ; Read a character into char
   cmp byte [char], 0ah ; Have we reached the end of the line?
   je check42 ; If so, check if 42 was entered
   mov eax, buf ; Point eax at the start of the buffer
   add ax, word [count] ; Add the current number of characters to eax
   mov bx, [char] ; Copy the character to the bx register
   mov [eax], bl ; Put the character to the end of the buffer
   inc word [count] ; Increment the count (number of characters)
   jmp _start ; Loop through
check42:
   mov ax, word [buf] ; We're assuming the number is two digits or less
   cmp ax, 3234h ; Is it 42?
   je end ; If so, end
   call writeln ; If not, write out the character
   mov word [count], 0 ; Reset the count
   jmp _start ; Loop
end:
   mov eax, 01h ; sys_end
   mov ebx, 00h ; Error number
   int 80h ; Make sys call
readchar:
   mov eax, 03h ; sys_read
   mov ebx, 00h ; stdin
   mov ecx, char ; Where to put the input
   mov edx, 01h ; How many characters to read?
   int 80h
   ret
writeln:
   mov eax, 04h ; sys_write
   mov ebx, 01h ; stdout
   mov ecx, buf ; Where to read from
   mov edx, 0 ; Clear edx
   mov dx, word [count] ; Copy count into the lower part of edx
   int 80h
   mov eax, 04h ; sys_write
   mov ebx, 01h ; stdout
   mov ecx, cr ; Carriage return
   mov edx, 01h ; One character
   int 80h
   ret
SECTION .data
   char db 0
   count dw 0
   cr db 0ah
SECTION .bss
   buf resb 255