你有两个问题:
-
如何解析字符串。
如果字符串 si 始终采用 gg/mm/aaaa hh:mm:ss 格式,您可以使用固定索引访问其部分。所以年份可以在索引 [6]、[7]、[8] 和 [9] 中找到。
-
如何将字符串转换为整数。
您可以阅读有关算法的信息。它们并不复杂,只是解释或多或少复杂。在这里,您只有两种数字:两位数字和四位数字。我建议获取值(即整数),就像您向小孩解释的那样:一年的值是 [first digit = "thousands"]*1000 + [second digit = "hundreds"]*100 + [third数字 = “十”]*10 + [第四位 = “单位”]。两位数的值为 [first digit = "tens"]*10 + [second digit = "units"]。
另外,我看到了一个初学者的错误。 AL 是AX 的一部分@ 是EAX 的一部分。如果您更改AL(例如mov al, date[0]),您也会更改EAX。
#include <stdio.h>
int main ( void )
{
// Input
char date[] = "20/06/1999 13:23:47"; //gg/mm/aaaa hh:mm:ss
// Output
unsigned short day;
unsigned short month;
unsigned short year;
unsigned short hour;
unsigned short minute;
unsigned short second;
__asm
{
mov cl, 10 ; Multiplicator
mov al, date[0] ; First digit
and al, 0Fh ; Isolate the decimal number of the ASCII character
mul cl ; AX = AL * CL => tens
mov dl, date[1] ; Second digit
and dx, 0Fh ; Isolate the decimal number of the ASCII character
add ax, dx ; Add the tens and the units
mov [day],ax ; Save the result
mov al, date[3] ; First digit
and al, 0Fh ; Isolate the decimal number of the ASCII character
mul cl ; AX = AL * CL
mov dl, date[4] ; Second digit
and dx, 0Fh ; Isolate the decimal number of the ASCII character
add ax, dx ; Add the tens and the units
mov [month],ax ; Save the result
movzx ax, byte ptr date[6] ; Load one byte into a word register
and al, 0Fh ; Isolate the decimal number of the ASCII character
imul dx, ax, 1000 ; DX = AX * 1000 => millenium
movzx ax, byte ptr date[7] ; Load one byte into a word register
and al, 0Fh ; Isolate the decimal number of the ASCII character
imul ax, ax, 100 ; AX = AX * 100
add dx, ax ; Add it to the millenium => 1900
movzx ax, byte ptr date[8] ; Load one byte into a word register
and al, 0Fh ; Isolate the decimal number of the ASCII character
imul ax, ax, 10
add dx, ax ; Add it => 1990
movzx ax, byte ptr date[9] ; Load one byte into a word register
and al, 0Fh ; Isolate the decimal number of the ASCII character
add dx, ax ; Add it => 1999
mov [year], dx ; Save the result (DX!)
mov al, date[11] ; First digit
and al, 0Fh ; Isolate the decimal number of the ASCII character
mul cl ; AX = AL * CL
mov dl, date[12] ; Second digit
and dx, 0Fh ; Isolate the decimal number of the ASCII character
add ax, dx ; Add the tens and the units
mov [hour], ax ; Save the result
mov al, date[14] ; First digit
and al, 0Fh ; Isolate the decimal number of the ASCII character
mul cl ; AX = AL * CL
mov dl, date[15] ; Second digit
and dx, 0Fh ; Isolate the decimal number of the ASCII character
add ax, dx ; Add the tens and the units
mov [minute],ax ; Save the result
mov al, date[17] ; First digit
and al, 0Fh ; Isolate the decimal number of the ASCII character
mul cl ; AX = AL * CL
mov dl, date[18] ; Second digit
and dx, 0Fh ; Isolate the decimal number of the ASCII character
add ax, dx ; Add the tens and the units
mov [second],ax ; Save the result
}
// Stampa su video
printf("day: %i month: %i year: %i hour: %i minute: %i second: %i \n", day,month,year,hour, minute, second);
// printf("press a key to go");
// getchar();
return 0;
}
从 ASCII 字符中分离出可计算的数字。当您从字符串中获取数字时,您会得到 ASCII 字符,例如“20”是 0x32 和 0x30。对于像 2 * 10 + 0 这样的算术,您需要“裸”十进制数。 ASCII 数字位于 0x30 和 0x39 之间的表中。如果你能去掉那个十六进制数中的第一个“3”,你就会得到一个计算机可以计算的数字。这就是AND 0Fh 的目的。前 4 位由 0000 与 - 它们变为 0。最后 4 位由 1111 (F) 与 - 它们保持其原始状态。 0x35 AND 0x0F = 0x05。当您在任何地方看到 48 或 0x30 的减法时 - 这追求相同的目的。