assembly - Using EMU8086, is there a direct way to print a variable's hex value? -


i'm using emu8086 on windows 7 hp x64, intel i3-2330m pc.

i have spent 2 weeks researching , tinkering assembly language program in effort print hex value of integer entered user. have found nothing directly printing value memory location. can, if necessary, write code string conversion hex. however, attempting avoid lines of code this. (i think called optimizing.)

my research has lead me comment on site corbin says:

very unpleasantly. you'll have determine value of decimal string ("100"), make new string same value in hex ("0x64"). it's same algorithm use conversion in head; it's bit more complicated since have deal ascii encoding on top of things.

none of has directed me manner in desire perform task.

i have written , tested code incrementally, receiving desired results. watching variable , viewing cx register, can confirm values values want print.

i including code here. ugly , over-commented. commenting benefit me learn/remember have done progress.

 ; program name: convert_decimal_to_hex  ; w k  ; august, 2015  ; program accepts integer between 1 , 10000 ,  ; prints hexadecimal value console  ;  ;  #make_com# ;instruct compiler make com file  include emu8086.inc    ;use include file macros  org 100h   ;directive com program, set offset 100h  ;  mov dx, offset msg1    ;moves value of msg1 data register  ;                         mov ah, 9  ;moves value 9 high accumulator register             ;to used interrupt  int 21h    ;invokes interrupt print string dos window  ;  mov dx,13  ;these lines   mov ah,2  ;create   int 21h   ;newline   mov dx,10 ;and   mov ah,2  ;move cursor   int 21h   ;to newline  ;  call scan_num  ;calls macro scan numeric keyboard input                 ;error catching included in macro                 ;number stored in cx    mov dx,13 ;these lines   mov ah,2  ;create   int 21h   ;newline   mov dx,10 ;and   mov ah,2  ;move cursor   int 21h   ;to newline  ;    mov dx, offset msg2    ;moves value of msg2 data register  mov ax, cx ;copies scanned number ax printed             ;as part of message  ;mov ah, 9  ;moves value 9 high accumulator register              ;to used interrupt  ;int 21h    ;invokes interrupt print string dos window     call print_num_uns  ;prints hexadecimal value @ end of msg2  mov convertednumber, cx  ;                         mov ah, 9  ;moves value 9 high accumulator register             ;to used interrupt  int 21h    ;invokes interrupt print string dos window  ;  mov ax, 0  ;wait key  int 16h    ;prevents window closing  ;  msg1 db "type integer between 1 , 10000, , press enter. $"         ;declare string variable   msg2 db " in hexadecimal is: $"   ;  define_scan_num    ;defines macro include file                     ;gets multidigit signed number keyboard  define_print_num_uns   ;defines macro include file                         ;prints unsigned number in ax  ;                                                     convertednumber dw 0   ;declare variable  ;  ret    ;return operating system  end    ;directive stop compiler 

is there procedure directly print variable value or cx register value without writing lines of code convert string value input user? references articles, code examples, or other helpful advice appreciated.

dos provides interrupts write characters standard output (int 21h / ah=2 single character, , int 21h / ah=9 $-terminated string of characters).

so will have convert number want print string first, in whatever base wish display number in. fortunately that's easy do. below example of how 1 print 32-bit hexadecimal numbers (i'll leave exercise strip leading zeroes):

; input: ; eax = value print ; print_hex:     mov cx,8        ; print 8 hex digits (= 32 bits)     .print_digit:         rol eax,4   ; move left-most digit least significant 4 bits         mov dl,al         , dl,0xf  ; isolate hex digit want print         add dl,'0'  ; , convert character..         cmp dl,'9'  ; ...         jbe .ok     ; ...         add dl,7    ; ... (for 'a'..'f')     .ok:            ; ...         push eax    ; save eax on stack temporarily         mov ah,2    ; int 21h / ah=2: write character stdout         int 0x21         pop eax     ; restore eax         loop .print_digit         ret 

printing numbers in base 10 or base 2 not different. it's when deal bases powers of 2 (like 16 or 2), can use shifts (or rotates in code) , ands rather division , remainders.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -