I am writing a very simple program that subtracts 16 bit integers. After the subtractions I want to view the registers' final values by using DumpRegs. The information displayed seems accurate for EAX, but EBX and ECX have values that don't seem to be correct. Why is this so?

TITLE Subtract Three          (subtractThree.asm)

;Description: 
;	This program subtracts 3 integers using only 16 bit registers.

INCLUDE Irvine32.inc

.code
main PROC

	mov  ax,8000h	        ;ax = 8000h
	mov  bx,2000h		;bx = 2000h
	mov  cx,3000h		;cx = 3000h
	sub  ax,bx		;subtract bx from ax, ax = 6000h
	sub  ax,cx		;subtract cx from ax, ax = 3000h
	sub  cx,bx		;subtract bx from cx, cx = 1000h
	
	call	DumpRegs		;display registers	
	
	exit				;halt program

main ENDP				;end procedure	
END main				;end program

Here is the output...


EAX=00003000 EBX=7FFD2000 ECX=00121000 EDX=7C90E514
ESI=01CB4228 EDI=D8DB1C09 EBP=0012FFF0 ESP=0012FFC4
EIP=0040102A EFL=00000206 CF=0 SF=0 ZF=0 OF=0 AF=0 PF=1

Press any key to continue . . .

Recommended Answers

All 2 Replies

Hi

Glad to meet you again.

In your code you only manipulate the lower 16 bits of eax, ebx and ecx by subtracting 16 bit regs. So the upper 16 bits contain random digits.

You should clear them:

mov eax, 0
mov ebx, 0
mov ecx, 0
;; then your subtractions here

However, only sub cx, bx will be correct for 8000H is already a negative number ! See: 8000H = 1000 0000 0000 0000B. The leading 1 (MSB) tells that this is a negative number in two's complement. If you deduct a positive number 2000H from a negative number 8000H the result is more negative. If not, then an overflow has occurred. Can you figure out the correct result of 8000H - 2000H ? Indeed, an overflow and a funny result will occur. (You may check OF flag.)

So simply change 8000H into 5000H (0101 0000 0000 0000) which is a positive number.

-- tesu

Member Avatar for Josue198s

thanks man this a nice code....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.