简体   繁体   中英

How do i Print out a number using fasm assembler?

I am really new to assembly programming and i am learning to experiment with the few things i am learning by myself and class. So, my goal is to display a number stored in a register. When i run the program, it displays the character value of the number, so if i were to display the number itself how would i do that. Here is my code, please kindly suggest me where i made my mistake. Until now we have been taught the move instructions and few other basic things in assembly.

#fasm#

mov ah,2

mov bh,66
add bh,1

mov dl,bh


int 21h
int 20h
use16 ;generate 16bit opcodes
org 0100h ;code offset for .COM program

 mov ax,1976 ;ax = number to display

 xor cx,cx ;cx = count = 0
 mov bx,10 ;bx = number base = 10
.stack:
 xor dx,dx ;dx:ax for div
 div bx ;dx:ax = dx:ax div base
 add dx,'0' ;dx into ascii
 push dx ;stack it
 inc cx ;increment counter
 test ax,ax ;do again if not 0
 jnz .stack
 mov ah,02h ;DOS 1+ - WRITE CHARACTER TO STANDARD OUTPUT
.write:
 pop dx ;unstack it
 int 21h ;write
 loop .write ;for each in count

 mov ah,08h ;DOS 1+ - CHARACTER INPUT WITHOUT ECHO
 int 21h ;read

 int 20h ;DOS 1+ - TERMINATE PROGRAM

You can use win32 api (example below). I would suggest you to search for Iczelion tutorials. They're in MASM. FASM Iczelion examples are here .


format PE GUI 4.0
entry start

; macros for `invoke`, `cinvoke`, ...
include 'win32ax.inc'

; code section
section '.text' code readable writable executable
     ; text buffer for the number to display
     buffer  rb 64  ; 64 bytes

     ; program start
  start:

     ; our number
     mov     eax, 1234

     ; printing EAX to buffer
     cinvoke wsprintf, buffer, '%d', eax

     ; terminate string with zero
     mov     [buffer + eax], 0

     ; showing message
     invoke  MessageBox, 0, buffer, 'result', MB_OK

     ; calling exit
     invoke  ExitProcess, 0

section '.idata' import readable writable
     library   kernel32, 'KERNEL32.DLL',\
               user32,   'USER32.DLL'

     include   'api\kernel32.inc'
     include   'api\user32.inc'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM