如何使用fasm汇编程序打印数字?

时间:2015-06-20 17:10:16

标签: assembly x86 fasm

我是汇编编程的新手,我正在学习自己和课堂上学习的一些东西。所以,我的目标是显示存储在寄存器中的数字。当我运行程序时,它显示数字的字符值,所以如果我要显示数字本身我将如何做到这一点。这是我的代码,请建议我在哪里弄错了。到目前为止,我们已经学会了移动指令和其他一些基本的东西。

#fasm#

mov ah,2

mov bh,66
add bh,1

mov dl,bh


int 21h
int 20h

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用win32 api(以下示例)。我建议你搜索Iczelion教程。他们在MASM。 FASM Iczelion示例为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'
相关问题