0

Write and run a program that adds 5 bytes of data and saves the result. The data should be the following hex numbers: 25, 12, 15, IF, and 2B. Show the program and the snapshot of the output. The beginning of the program is given:

.MODEL SMALL  
.STACK 0100h  
.DATA  
DATA_IN DB 25H, 12H, 15H, 1FH, 2BH 
sum db?

I'm not able to get the output in hexadecimal. I have tried this code but still cant get the output I want:

.model small

.stack 100h
.data
data_in db 25H, 12H, 15H, 1FH, 2BH 
sum db ?
msg1    db 'The result is : $' ;

.code
mov ax,@data
mov ds,ax

lea si,data_in
mov al,[si]
mov cx, 4

again:
inc si
add al, [si]
loop again
mov sum,al


lea dx, msg1
mov ah,09h
int 21h
mov ah, 2
mov dl, SUM
int 21h

mov ah,4ch ; end operation
int 21h
end
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    `sum` has the value `25H+12H+15H+1FH+2BH=96H=150`. When you write this character with [INT21h/AH=2](http://www.ctyme.com/intr/rb-2554.htm), it will be displayed as character **û** (depending on current console font). When you want **96** instead, it needs [conversion to hexadecimal string](https://stackoverflow.com/questions/18879672/printing-hex-values-in-x86-assembly). – vitsoft Dec 12 '22 at 10:53

1 Answers1

1

The key to printing hexadecimal bytes is remembering that the ASCII code for a number is its value plus 0x30. There are versions of this code that are far more optimized than this, but are harder to read and understand, so I'll give you the more readable version:

PrintHex:
;input: al = the byte you wish to print.
push ax

shr al,1
shr al,1
shr al,1
shr al,1     ;move the top 4 bits to the bottom.
call ShowHex
pop ax
and al,0Fh   ;now print the bottom 4 bits as ASCII.

;fallthrough is intentional - we want to run this code again and return.

ShowHex:
add al,30h       ;converts the digit to ASCII
cmp al,3Ah       ;if greater than A, we need letters A through F
jc noCorrectHex
    add al,7     ;3Ah becomes "A", 3Bh becomes "B", etc.
noCorrectHex:

mov dl,al
mov ah,2
int 21h      ;print
ret
puppydrum64
  • 1,598
  • 2
  • 15