Hi,
I need to write a program (on emu8086), which would convert hexadecimal number into decimal number. I have to do it until tomorrow, but I don't know where to start. Please, someone, help me to write that program. :crying:
11 replies to this topic
#1
Posted 10 October 2011 - 07:19 AM
|
|
|
#2
Posted 10 October 2011 - 11:29 AM
Well, first let me ask, do you know how to convert between them by hand? You can implement something similar in code.
---------- Post added at 03:29 PM ---------- Previous post was at 03:20 PM ----------
And was it specified how long the hexadecimal number is?
---------- Post added at 03:29 PM ---------- Previous post was at 03:20 PM ----------
And was it specified how long the hexadecimal number is?
Latinamne loqueris?
#3
Posted 10 October 2011 - 05:22 PM
Here, see if you can convert this to assembly language:
I tested it, and it worked. I can't post the assembly code, or that would be cheating. But please, do read the comments and try to understand why it does what it does, because understanding the code and the process is more important than having the code.
Here, I'll even start you off:
short hex2dec (char * hex){
short rvalue; // Used for return value.
char * a; // Just some pointer; you can use BX for this.
int i; // This is just an integer; using SI would be great for this.
char c; // You can just use CL or something for this.
a= hex; // Set the pointer to the first parameter.
rvalue= 0; // Initialize the return value to 0.
// Since we're dealing with WORD values, we'll only look at 4 of the
// characters, because that's the most we can fit in the return value.
for (i= 0; i < 4; i++){
c= hex2char (*a); // Get the value from the character (ie '4' -> 4, 'd' -> 13, 'F' -> 15, etc.).
if (c == -1) break; // If not a valid hexadecimal character, finish the loop.
rvalue= (rvalue<<4) | c;
a++; // Don't forget to increment the pointer, so that we're looking at the next
// hexadecimal digit.
}
return rvalue;
}
char hex2char (char hex){
char a; // Just some character; you can use AL for this.
a= hex;
a -= 48; // 48 is the ASCII code for '0', so subtracting
// 48 from it would convert '0' to 0, '1' to 1, etc.,
// all the way up through 9.
if (a < 0) return -1; // If below 0, return error.
if (a < 10) return a; // If 0 through 9, return that value.
// Otherwise, we'll have to process it further.
// 'A' is 65, so we'll have to subtract 65 from
// the value. But since we already subtracted 48, earlier,
// we'll have to subtract (65 - 48), which is 17.
a -= 17;
if (a < 6) return a + 10; // 5 + 65 = 'F'
// The highest value for this can be 5 (F), at this point.
// If it's not higher than 5, then we can return that number.
// In other words, we just subtracted 65, pretty much, from
// the character. That means 'A' would now, instead of being
// 65, would be 0, and 'B' would now, instead of being 66,
// be 1, and so on. 'F' would be 5. That means if the number
// we're looking at is greater than 5 (F), then it's not in
// the range of hexadecimal characters.
// If not in range, then maybe it's a lower-case hexadecimal
// character that's in range. Let's check for that.
// Lower-case 'a' is 97, 'b' is 98, etc., so we'll have to
// subtract 97 from the character. Since we already subtracted
// 65, we'll have to subtract (97 - 65) from the character,
// which is 32.
a -= 32;
// Now the same as the statement before the previous statement.
// We check if it's in range, and if so then we return 10 + the number,
// because 0 is 'a', and 1 is 'b', etc., at this point, for the
// character. But really, 'a' hexadecimal means 10, so if it's 0
// then we'll just have to add 10 to the number to make it to be
// the right value.
if (a < 6) return a + 10; // 5 + 97 = 'f'
// In all other cases, it's not a hexadecimal character.
return -1;
}
I tested it, and it worked. I can't post the assembly code, or that would be cheating. But please, do read the comments and try to understand why it does what it does, because understanding the code and the process is more important than having the code.
Here, I'll even start you off:
hex2dec: enter 0, 0 push bx push si mov ax, word [bp+4] mov bx, ax ;; Your code here... pop si pop bx leave ret 2
#4
Posted 10 October 2011 - 07:26 PM
ENTER and LEAVE weren't on the 8086, so I doubt they will be on the EMU8086 program he is using.
Latinamne loqueris?
#5
Posted 10 October 2011 - 09:54 PM
Oh, page 21 of the pink shirt book, that's right.
Those four instructions are not supported on the 8088; but here's mostly what they decode to:
ENTER #, 0 Note: I forgot what the second operand of ENTER is for; how responsible of me :)
LEAVE
PUSHA
And here's POPA
So actually this is what the code could start with:
One thing to note, though, is that you are more restricted as for effective addressing on 8086 than you would have been on 386. On 386, this is allowed:
On 8086, you can't do this:
I think you can use immediate offsets with any memory accesses, also. Like 'mov ax, [si+18]' or 'mov ax, [bx+si+24]' or something.
Those four instructions are not supported on the 8088; but here's mostly what they decode to:
ENTER #, 0 Note: I forgot what the second operand of ENTER is for; how responsible of me :)
push bp mov bp, sp sub sp, #
LEAVE
mov sp, bp pop bp
PUSHA
push eax push ecx push edx push ebx push esp push ebp push esi push ediOops! I forgot that this isn't 386; :c-whistle: It's actually this:
push ax push cx push dx push bx push sp push bp push si push di
And here's POPA
pop di pop si pop bp add sp, 2 pop bx pop dx pop cx pop ax
So actually this is what the code could start with:
hex2dec: push bp mov bp, sp sub sp, 0 ;; That last line is extra; just showing how it works. push bx push si mov ax, word [bp+4] mov bx, ax ;; Your own code here... pop si pop bx mov sp, bp pop bp ret 2
One thing to note, though, is that you are more restricted as for effective addressing on 8086 than you would have been on 386. On 386, this is allowed:
mov eax, [ebx+ecx]
On 8086, you can't do this:
mov ax, [bx+cx]; you have to do this:
mov ax, [bx+si] [COLOR=#4444FF]OR[/COLOR] mov ax, [bx+di] [COLOR=#4444FF]OR[/COLOR] mov ax, [bp+si] [COLOR=#4444FF]OR[/COLOR] mov ax, [bp+di]
I think you can use immediate offsets with any memory accesses, also. Like 'mov ax, [si+18]' or 'mov ax, [bx+si+24]' or something.
#6
Posted 11 October 2011 - 11:52 AM
All those addressing rules are what caused me to hate x86 real mode programming...
Latinamne loqueris?
#7
Posted 11 October 2011 - 02:59 PM
It certainly is easier to program 386 and later. Though out-of-order execution is a bit confusing; I mean, if you have 'mov eax, [ebx]' and then 'mov ecx, eax' then the latter would be executed first? But then how does it all work out?
#8
Posted 11 October 2011 - 03:03 PM
All that out-of-order stuff is handled by the CPU, it's all transparent to the programmer (fortunately). The CPU basically tries to determine which instructions are dependent on which (all this happens in an instant) and when, say, waiting for input from RAM, it'll run another instruction that doesn't depend on the data change created by the previous instruction. Crazy stuff, huh?
Latinamne loqueris?
#9
Posted 11 October 2011 - 03:09 PM
I still am not totally sure that it would be able to track what depends on what. I was thinking maybe there could be an anti-virus that would look at EXEs and determine what they're trying to do by looking at what comes from where, but that would be complicated and slow.
* * *
Though this is getting a little off-topic; maybe we should start a new thread for this CPU talk.
* * *
Though this is getting a little off-topic; maybe we should start a new thread for this CPU talk.
#10
Posted 11 October 2011 - 03:41 PM
You'd be surprised at what today's CPUs are capable of. I guess it was an overstatement to say "track", but it fetches an instruction and checks whether any of the other instructions waiting for data affect any of the data that the current instruction depends on. And yes, if we decide to continue on, that would be a good idea.
Latinamne loqueris?
#11
Posted 19 October 2011 - 01:54 PM
If you two have started a new thread I'd be happy to contribute information.
sudo rm -rf /
#12
Posted 19 October 2011 - 04:29 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









