Need Help With Assembly Code

Status
Not open for further replies.

DartX2

Smack-Fu Master, in training
73
I'm getting my butt kicked by a simple assignment for my x86 programming class. Since this is a class, Im not asking for people to solve the problem for me, I just need a sanity check and someone to point me in the right direction.

The problem is a simple one: convert a 4-digit hex number into decimal. Heres the problem code:


mov AL, value4
mov AH, value2

mov BL, value3
mov BH, value1

mov CL, 04h
SHL AH, CL
SHL AL, CL

add AX, BX

; add AH, value4
; add AL, value2

jmp T104


negative:
; Print minus sign, complement, etc

jmp endit

T104:
CMP AX, 10000
jb T103
sub AX, 10000
inc t5
jmp T104

T103:
CMP AX, 1000
jb T102
sub AX, 1000
inc t4
jmp T103

T102:
CMP AX, 100
jb T101
sub AX, 100
inc t3
jmp T102

T101:
CMP AX, 10
jb T100
sub AX, 10
inc t2
jmp T101

T100:
CMP AX, 1
jb section2
sub AX, 1
inc t1
jmp T100

This code is supposed to increment a series of counters representing each power of 10.

Can anyone look at this and help? My suspicion is that the problem is before the Txxx labels.

Jason
 

Ezran

Wise, Aged Ars Veteran
137
code:<HR>
Code:
mov AL, value4
mov AH, value2<P>mov BL, value3
mov BH, value1<P>mov CL, 04h
SHL AH, CL
SHL AL, CL<P>add AX, BX

It looks to me like you're ending up with the values in this order:
val2, val1, val4, val3

I don't think that's what you're going for. It should either be 1,2,3,4 or 4,3,2,1, depending what the values mean. It looks like you're attempting to deal with big- vs. little-endian numbers, so I'll assume I don't need to explain that at all.

Oh, and I don't remember for sure, but you might want to check that CL is not destroyed when you do the first SHL.

I haven't looked through any more of the code yet, but let me know if this helps at all.

-- Ezran
 
mov AL, value4
mov AH, value2

mov BL, value3
mov BH, value1

mov CL, 04h
SHL AH, CL
SHL AL, CL

add AX, BX

; add AH, value4
; add AL, value2
Seems like you changed your mind along the way and introduced a bug. If I get this right, you should now be shifting BH and BL, not AH and AL. I'm assuming here your hex number is (value1)(value2)(value3)(value4), which makes sense with the ordering of values in your H and L registers. Value 2 and 4 should stay as such in AH and AL. Value 1 and 3 need to be shifted left (or multiplied by 16) to have the proper weights.

Edit: Looks like Ezran got there first ;)
 

DartX2

Smack-Fu Master, in training
73
Originally posted by hanser:
I can't help you (sorry), but a quick question for you. What year in college are you, where do you go, and I assume your major is CS?


(Just trying to get a handle on when I will be doing things similar to what you're doing. :))

Hanser,

This is considered a Sophomore-level class (CSCI 241) at Texas A&M-Commerce (I would never willingly go to the real Texas A&M, being a Longhorn fan).

Yes, my major is CS. Since I only go to school part time, maybe I'll finish up within 10 years. Then again, maybe I should have majored in Food Prep and got a show on Food TV... right before Iron Chef... *rambles off into incoherance as he goes back to his homework*
 

Clintology

Ars Legatus Legionis
15,566
Subscriptor
Originally posted by hanser:
I can't help you (sorry), but a quick question for you. What year in college are you, where do you go, and I assume your major is CS?


(Just trying to get a handle on when I will be doing things similar to what you're doing. :))

Don't know if anyone cares, but I'll start into assembly programming next semester (not x86 of course) but it'll be my 3rd semester of Electrical Engineering Technology courses. ;)

=clint
 

DartX2

Smack-Fu Master, in training
73
Okay, thanks guys, its working right now (Im still trying to convince myself of how it actually works).

My next thing is to check for negatives (e.g. two's complement). If the most significant digit is a 1, then via the rules of signed arithmatic, the number is negative (meaning it needs to go through the complement process).

Here's how I intend to check that most significant digit:

code:<HR>
Code:
mov DX, BX
mov CL, 0Fh
shr DX, CL ; Shift the value in DX 15 bits right<P>cmp DX, 1
je negative<P>:negative
sub BX, (whatever the most significant digit is in hex)
jmp T105

etc, etc

Am I on the wrong track here?

Jason
 
code:<HR>
Code:
:negative
sub BX, (whatever the most significant digit is in hex)
If you're going to use sub, you might as well use neg (I think this exists anyway). If you're going to do the complement the traditional way, and I belive it's:
code:<HR>
Code:
not BX   <-- not sure about that mnemonic on x86, I mean to do a simple boolean negation (turn 0s to 1s and 1s to 0s)
inc BX
 
Status
Not open for further replies.